NSNotificationCenter應用總結

一般咱們在 iOS 中發生什麼事件時該作什麼是由 Delegate 實現的,例如 View 加載完後會觸發 viewDidLoad。  Apple 還爲咱們提供了另外一種通知響應方式,那就是 NSNotification,系統中(UIKeyboardDidShowNotification 等) 以及某些第三方組件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。html

NSNotificationCenter 較之於 Delegate 能夠實現更大的跨度的通訊機制,能夠爲兩個無引用關係的兩個對象進行通訊。NSNotificationCenter 的通訊原理使用了觀察者模式:app

1. NSNotificationCenter 註冊觀察者對某個事件(以字符串命名)感興趣,及該事件觸發時該執行的 Selector 或 Block
2. NSNotificationCenter 在某個時機激發事件(以字符串命名)
3. 觀察者在收到感興趣的事件時,執行相應的 Selector 或 Block
 
 
1、瞭解幾個相關的類
一、NSNotification
這個類能夠理解爲一個消息對象,其中有三個成員變量。
這個成員變量是這個消息對象的惟一標識,用於辨別消息對象。
@property (readonly, copy) NSString *name;
 
這個成員變量定義一個對象,能夠理解爲針對某一個對象的消息。
@property (readonly, retain) id object;
 
這個成員變量是一個字典,能夠用其來進行傳值。
@property (readonly, copy) NSDictionary *userInfo;
 
NSNotification的初始化方法:
- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
注意:官方文檔有明確的說明,不可使用init進行初始化
 
二、NSNotificationCenter
這個類是一個通知中心,使用單例設計,每一個應用程序都會有一個默認的通知中心。用於調度通知的發送的接受。
添加一個觀察者,能夠爲它指定一個方法,名字和對象。接受到通知時,執行方法。
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
 
發送通知消息的方法
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
 
移除觀察者的方法
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
 
幾點注意:
一、若是發送的通知指定了object對象,那麼觀察者接收的通知設置的object對象與其同樣,纔會接收到通知,可是接收通知若是將這個參數設置爲了nil,則會接收一切通知。
二、觀察者的SEL函數指針能夠有一個參數,參數就是發送的死奧西對象自己,能夠經過這個參數取到消息對象的userInfo,實現傳值。
 
實例:
@interface classB : NSObject

-(void) testNotification;

@end

@implementation classB

-(void) testNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callback:) name:@"TEST" object:nil];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"TEST" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        NSLog(@"%@", [note name]);
        NSLog(@"%@", [note object]);
        NSLog(@"%@", [note userInfo]);
    }];
}

-(void) callback:(id)notification{
    [self testString];
    NSDictionary *info = [notification userInfo];
    
    [info enumerateKeysAndObjectsUsingBlock:
     ^(id key, id object, BOOL *stop){
         //do sth
        NSLog(@"%@ = %@", key, object);
     }];
}

@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //test notification
        classB *b = [[classB alloc] init];
        [b testNotification];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TEST" object:nil userInfo:@{@"a":@"hello",@"b":@123}];
        
    }
    return 0;
}

運行結果:函數

2016-05-06 11:24:05.589 test2[65542:7170843] a = hello
2016-05-06 11:24:05.589 test2[65542:7170843] b = 123
2016-05-06 11:24:05.589 test2[65542:7170843] TEST
2016-05-06 11:24:05.589 test2[65542:7170843] (null)
2016-05-06 11:24:05.589 test2[65542:7170843] {
    a = hello;
    b = 123;
}
相關文章
相關標籤/搜索