Skip to content

A category on NSNotificationCenter to make creating notifications with blocks a little cleaner.

Notifications You must be signed in to change notification settings

kris2point0/NSNotificationCenter-CLFBlockNotifications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

NSNotificationCenter-CLFBlockNotifications

!!!

This category has been replaced by NSObject+CLFNotificationObserver and is no longer being maintained.

!!!

A category on NSNotificationCenter to make creating notifications with blocks a little cleaner.

Motivation

If you're like me, you prefer to set up your notifications using blocks instead of selectors. Although I prefer this method, I wanted a little cleaner syntax than having to use:

// Sign up for notification
self.myNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:MyNotificationName
												 					    		object:nil
												   								 queue:nil
																    		usingBlock:^(NSNotification *note) {
    [self doSomethingAboutIt];
}];

// Remove observer
[[NSNotifcationCenter defaultCenter] removeObserver:self.myNotificationObserver];
self.myNotificationObserver = nil;

So I created this category to clean it up a little bit. Now you can get the same result as the above code with this:

// Sign up for notification
[[NSNotificationCenter defaultCenter] addObserverAtAddress:&_myNotificationObserver
												   forName:MyNotificationName
												usingBlock:^(NSNotification *note) {
    [self doSomethingAboutIt];
}];

// Remove observer
[[NSNotificationCenter defaultCenter] removeObserverAtAddress:&_myNotificationObserver];

There are also methods to register for notifications with an object, with a queue, or with both.

Example Usage

@interface FooViewController ()
{
	id _myNotificationObserver;
}

@end


@implementation FooViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserverAtAddress:&_myNotificationObserver
    												   forName:MyNotificationName
    											    usingBlock:^(NSNotification *note) {
        NSLog(@"Received notification: %@", note);
    }];
    // _myNotificationObserver is now set
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserverAtAddress:&_myNotificationObserver];
    // _myNotificationObserver is now nil
}

@end

License Info

Released under an MIT License.

Copyright (c) 2013 Chris Flesner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A category on NSNotificationCenter to make creating notifications with blocks a little cleaner.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published