Showing posts with label NSNotificationCenter example. Show all posts
Showing posts with label NSNotificationCenter example. Show all posts

Saturday, April 27, 2013

NSNotificationCenter removeObserver example ios


removeObserver:

Removes all the entries specifying a given observer from the receiver’s dispatch table.
- (void)removeObserver:(id)notificationObserver
Parameters
notificationObserver
The observer to remove. Must not be nil.
Discussion
Be sure to invoke this method (or removeObserver :name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.
Example of [NSNotificationCenter removeObserver]
The following example shows how you can register to receive locale change notifications.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
    queue:mainQueue usingBlock:^(NSNotification *note) {
 
        NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
    }];
To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified byaddObserverForName:object:queue:usingBlock: is deallocated.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver :self.localeChangeObserver];

NSNotificationCenter postNotificationName example ios


postNotificationName :object:

Creates a notification with a given name and sender and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
Parameters
notificationName
The name of the notification.
notificationSender
The object posting the notification.
Discussion of [NSNotificationCenter postNotificationName]
This method invokes postNotificationName :object:userInfo: with a userInfo argument of nil.
Example of [NSNotificationCenter postNotificationName]

[[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:self userInfo:itemDetails ];


- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.userInfo);
      NSLog(@"%@", [notification.userInfo objectForKey:@"HelpTopic"]);    
}

NSNotificationCenter postNotification example ios


postNotification:

Posts a given notification to the receiver.
- (void)postNotification:(NSNotification *)notification
Parameters
notification
The notification to post. This value must not be nil.
Discussion of [NSNotificationCenter postNotification]
You can create a notification with the NSNotification class method notificationWithName:object: ornotificationWithName:object:userInfo:. An exception is raised if notification is nil.
Example of [NSNotificationCenter postNotification]

[[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:self userInfo:itemDetails ];


- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.userInfo);
      NSLog(@"%@", [notification.userInfo objectForKey:@"HelpTopic"]);    
}

NSNotificationCenter addObserverForName example ios


addObserverForName :object:queue:usingBlock:

Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queueusingBlock:(void (^)(NSNotification *))block
Parameters
name
The name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue.
If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.[NSNotificationCenter addObserverForName]
obj
The object whose notifications you want to add the block to the operation queue.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.
queue
The operation queue to which block should be added.
If you pass nil, the block is run synchronously on the posting thread.
block
The block to be executed when the notification is received.
The block is copied by the notification center and (the copy) held until the observer registration is removed.
The block takes one argument:
notification
The notification.
Return Value of [NSNotificationCenter addObserverForName]
An opaque object to act as the observer.
Discussion
If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).
Example of [NSNotificationCenter addObserverForName]
The following example shows how you can register to receive locale change notifications.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.localeChangeObserver = [center addObserverForName :NSCurrentLocaleDidChangeNotification object:nil
    queue:mainQueue usingBlock:^(NSNotification *note) {
 
        NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
    }];
To unregister observations, you pass the object returned by this method to removeObserver:. You must invokeremoveObserver: or removeObserver:name:object: before any object specified by addObserverForName :object:queue:usingBlock: is deallocated.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];

NSNotificationCenter addObserver example ios


addObserver :selector:name:object:

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString*)notificationName object:(id)notificationSender
Parameters 
notificationObserver
Object registering as an observer. This value must not be nil.
notificationSelector
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).[NSNotificationCenter addObserver]
notificationName
The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.
notificationSender
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
Discussion
Be sure to invoke removeObserver: or removeObserver:name:object: before notificationObserver or any object specified in addObserver :selector:name:object: is deallocated.
Example of [NSNotificationCenter addObserver]
@implementation TestClass

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

    // Add this instance of TestClass as an observer of the TestNotification.
    // We tell the notification center to inform us of "TestNotification"
    // notifications using the receiveTestNotification: selector. By
    // specifying object:nil, we tell the notification center that we are not
    // interested in who posted the notification. If you provided an actual
    // object rather than nil, the notification center will only notify you
    // when the notification was posted by that particular object.

    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTestNotification:) 
        name:@"TestNotification"
        object:nil];

    return self;
}

- (void) receiveTestNotification:(NSNotification *) notification
{
    // [notification name] should always be @"TestNotification"
    // unless you use this method for observation of other notifications
    // as well.

    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}

@end