Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinkirkup/3899330 to your computer and use it in GitHub Desktop.
Save kevinkirkup/3899330 to your computer and use it in GitHub Desktop.
Cocoa Notifications
// From: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter/
// Add Methods to post and handle notifications
@interface ViewController ()
- (void)postNotificationWithString:(NSString *)orientation;
- (void)useNotificationWithString:(NSNotification*)notification;
@end
/**
* Register for notifications.
*/
NSString *notificationName = @"MTPostNotificationTut";
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(useNotificationWithString:)
name:notificationName
object:nil];
/**
* Unregister from Notifications (ARC)
*/
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
* Post notification
*/
- (void)postNotificationWithString:(NSString *)orientation //post notification method and logic
{
NSString *notificationName = @"MTPostNotificationTut";
NSString *key = @"OrientationStringValue";
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:orientation forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dictionary];
}
/**
* Handle notification
*/
- (void)useNotificationWithString:(NSNotification *)notification
{
NSString *key = @"OrientationStringValue";
NSDictionary *dictionary = [notification userInfo];
NSString *stringValueToUse = [dictionary valueForKey:key];
NSLog(@"Device orientation --> %@",stringValueToUse);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment