天天看點

iOS 通知中心 NSNotificationCenter & NSNotification

通知中心是 Foundation 架構的一個子系統,它向應用程式中注冊為某個事件觀察者的所有對象廣播消息(即通知)。(從程式設計角度而言,它是 

NSNotificationCenter

 類的執行個體)。該事件可以是發生在應用程式中的任何事情,例如進入背景狀态,或者使用者開始在文本欄中鍵入。通知是告訴觀察者,事件已經發生或即将發生,是以讓觀察者有機會以合适的方式響應。通過通知中心來傳播通知,是增加應用程式對象間合作和内聚力的一種途徑。

iOS 通知中心 NSNotificationCenter & NSNotification

任何對象都可以觀察通知,但要做到這一點,該對象必須注冊,以接收通知。在注冊時,它必須指定選擇器,以确定由通知傳送所調用的方法;方法簽名必須隻有一個參數:通知對象。注冊後,觀察者也可以指定釋出對象。

(以上是官方文檔中的解釋)

------------------------------------------華麗的分割線----------------------------------------------------------

通知中心包括兩個重要的類:

(1)NSNotificationCenter: 實作NSNotificationCenter的原理是一個觀察者模式,獲得NSNotificationCenter的方法隻有一種,那就是[NSNotificationCenter defaultCenter] ,通過調用靜态方法defaultCenter就可以擷取這個通知中心的對象了,而NSNotificationCenter是一個單例模式,而這個通知中心的對象會一直存在于一個應用的生命周期。

  (2) NSNotification: 這是消息攜帶的載體,通過它,可以把消息内容傳遞給觀察者。

 (3)一個NSNotificationCenter可以有許多的通知消息NSNotification,對于每一個NSNotification可以有很多的觀察者Observer來接收通知。

通過上面的介紹可以知道,通過通知中心也可以實作不同類之間的參數傳遞。

注意當接受到消息後,不想再收到消息了,要把observer删除remove。

下面介紹如何使用(具體解釋看文檔)。

(1)NSNotification :用于建立傳遞的消息

Creating Notifications
+ notificationWithName:object:
+ notificationWithName:object:userInfo:
Getting Notification Information
– name
– object
– userInfo
           

(2) NSNotificationCenter :用于發送消息

Getting the Notification Center
+ defaultCenter
Managing Notification Observers
– addObserverForName:object:queue:usingBlock:
– addObserver:selector:name:object:
– removeObserver:
– removeObserver:name:object:
Posting Notifications
– postNotification:
– postNotificationName:object:
– postNotificationName:object:userInfo:
           

demo(例子中基本上涉及到以上所有的方法了):

定義了兩個類:Poster(發送消息)和Observer(接受消息)

Poster.h

#import <Foundation/Foundation.h>

@interface Poster : NSObject

-(void)postMessage;

@end
           

Poster.m

#import "Poster.h"

@implementation Poster

-(void)postMessage{
    
    //1.下面兩條語句等價
    //二者的差別是第一條語句是直接發送消息内容,第二條語句先建立一個消息,然後再發送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];
    
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];
    
    //2.下面兩條語句等價
    //參數:userInfo  --- Information about the the notification.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];
    
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];
}

@end
           

Observer.h

#import <Foundation/Foundation.h>

@interface Observer : NSObject

-(void)observer;

@end
           

Observer.m

#import "Observer.h"

@implementation Observer

-(void)observer {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];
    
    //删除所有的observer
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //删除名字為name的observer
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];
    
}

-(void)callBack1:(NSNotification*)notification{
    NSString *nameString = [notification name];
    NSString *objectString = [notification object];
    NSLog(@"name = %@,object = %@",nameString,objectString);
}

-(void)callBack2:(NSNotification*)notification{
    NSString *nameString = [notification name];
    NSString *objectString = [notification object];
    NSDictionary *dictionary = [notification userInfo];
    NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);
}

@end
           

main.m

#import <Foundation/Foundation.h>
#import "Poster.h"
#import "Observer.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //注意這裡的順序,要先observer,然後再poster
        Observer *myObserver = [[Observer alloc] init];
        [myObserver observer];
        
        Poster *poster = [[Poster alloc] init];
        [poster postMessage];
    }
    return 0;
}
           

好了,大概有關的内容都差不多了吧

iOS 通知中心 NSNotificationCenter &amp; NSNotification

附:

不過有個方法

addObserverForName:object:queue:usingBlock:

還不太懂如何使用,暫時放一下,有知道了麻煩評論告訴了。

繼續閱讀