Created
October 16, 2012 13:34
-
-
Save kevinkirkup/3899330 to your computer and use it in GitHub Desktop.
Cocoa Notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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