iOS NSNotification的使用

如果在一個類中想要執行另一個類中的方法可以使用通知
1.創建一個通知對象:
使用notificationWithName:object: 或者 notificationWithName:object:userInfo:

    NSNotification* notification = [NSNotification notificationWithName:kImageNotificationLoadFailed(connection.imageURL)
                                                                 object:self
                                                               userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,@"error",connection.imageURL,@"imageURL",nil]];

這裏需要注意的是,創建自己的通知並不是必須的。而是在創建自己的通知之前,採用NSNotificationCenter類的方法 postNotificationName:object: 和 postNotificationName:object:userInfo:更加便利的發出通知。這種情況,一般使用NSNotificationCenter的類方法defaultCenter就獲得默認的通知對象,這樣你就可以給該程序的默認通知中心發送通知了。注意:每一個程序都有一個自己的通知中心,即NSNotificationCenter對象。該對象採用單例設計模式,採用defaultCenter方法就可以獲得唯一的NSNotificationCenter對象。

注意:NSNotification對象是不可變的,因爲一旦創建,對象是不能更改的。

2.註冊通知:addObserver:selector:name:object:

可以看到除了添加觀察者之外,還有其接收到通知之後的執行方法入口,即selector的實參。因此爲了進行防禦式編程,最好先檢查觀察者是否定義了該方法。例如:添加觀察者代碼有

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(aWindowBecameMain:)
    name:NSWindowDidBecomeMainNotification object:nil];

這裏保證了self定義了aWindowBecameMain:方法。而對於一個任意的觀察者observer,不能保證其對應的selector有aWindowBecameMain:,可採用[observer respondsToSelector:@selector(aWindowBecameMain:)]] 進行檢查。所以完整的添加觀察者過程爲:

if([observer respondsToSelector:@selector(aWindowBecameMain:)]) {
        [[NSNotificationCenter defaultCenter] addObserver:observer selector:@selector(aWindowBecameMain:) name:NSWindowDidBecomeMainNotification object:nil];
    }

注意到addObserver:selector:name:object:不僅指定一個觀察者,指定通知中心發送給觀察者的消息,還有接收通知的名字,以及指定的對象。一般來說不需要指定name和object,但如果僅僅指定了一個object,觀察者將收到該對象的所有通知。例如將上面的代碼中name改爲nil,那麼觀察者將接收到object對象的所有消息,但是確定不了接收這些消息的順序。如果指指定一個通知名稱,觀察者將收到它每次發出的通知。例如,上面的代碼中object爲nil,那麼客戶對象(self)將收到任何對象發出NSWindowDidBecomeMainNotification通知。如果既沒有指定指定object,也沒有指定name,那麼該觀察者將收到所有對象的所有消息。

3.發送通知:postNotificationName:object:或者performSelectorOnMainThread:withObject:waitUntilDone:

例如程序可以實現將一個文本可以進行一系列的轉換,例如對於一個實例、RTF格式轉換成ASCII格式。而轉換在一個類(如Converter類)的對象中得到處理,在誠尋執行過程中可以加入或者刪除這種轉換。而且當添加或者刪除Converter操作時,你的程序可能需要通知其他的對象,但是這些Converter對象並不需要知道被通知對象是什麼,能幹什麼。你只需要聲明兩個通知,"ConverterAdded" 和 "ConverterRemoved",並且在某一事件發生時就發出這兩個通知。

當一個用戶安裝或者刪除一個Converter,它將發送下面的消息給通知中心:

[[NSNotificationCenter defaultCenter]
    postNotificationName:@"ConverterAdded" object:self];

或者是

[[NSNotificationCenter defaultCenter]
    postNotificationName:@"ConverterRemoved" object:self];

通知中心將會區分它們對象對這些通知感興趣並且通知他們。如果除了關心觀察者的通知名稱和觀察的對象,還關心其他之外的對象,那麼就把之外的對象放在通知的可選字典中,或者用方法postNotificationName:object:userInfo:。

而採用performSelectorOnMainThread:withObject:waitUntilDone:則是直接調用NSNotification的方法postNotification,而postNotificationName和object參數可以放到withObject的實參中。例如:

[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];//注意這裏的notification爲自定義的一個通知對象,可定義爲NSNotification* notification = [NSNotification notificationWithName:@"ConverterAdded"object:self];//那麼它的作用與上面的一致

4.移除通知:removeObserver:和removeObserver:name:object:

其中,removeObserver:是刪除通知中心保存的調度表一個觀察者的所有入口,而removeObserver:name:object:是刪除匹配了通知中心保存的調度表中觀察者的一個入口。

這個比較簡單,直接調用該方法就行。例如:

[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];

注意參數notificationObserver爲要刪除的觀察者,一定不能置爲nil。

PS:這裏簡單說一下通知中心保存的調度表。通知中心的調度表是給一些觀察者指定的一些通知集。一個通知集是通知中心發出的通知的子集。每個表的入口包含:

通知觀察者(必須要的)、通知名稱、通知的發送者。

下圖表示通知集中指定的通知的調用表入口的四種類型:

下圖表示四種觀察者的調度表


最後,提醒一下觀察者收到通知的順序是沒有定義的。同時通知發出和觀察的對象有可能是一樣的。通知中心同步轉發通知給觀察者,就是說 postNotification: 方法直到接收並處理完通知才返回值。要想異步的發送通知,可以使用NSNotificationQueue。在多線程編程中,通知一般是在一個發出通知的那個線程中轉發,但也可能是不在同一個線程中轉發通知。


通知 是在跳轉控制器之間常用的傳值代理方式,除了代理模式,通知更方便、便捷,一個簡單的Demo實現通知的跳轉傳值.

輸入所要發送的信息 ,同時將label的值通過button方法調用傳遞,

- (IBAction)buttonClick:(id)sender {

    //添加 字典,將label的值通過key值設置傳遞

    NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:self.textFieldOne.text,@"textOne",self.textFieldTwo.text,@"textTwo",nil];

    //創建通知

    NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:niluserInfo:dict];

    //通過通知中心發送通知

    [[NSNotificationCenter defaultCenter] postNotification:notification];

    [self.navigationController popViewControllerAnimated:YES];

 

}

在發送通知後,在所要接收的控制器中註冊通知監聽者,將通知發送的信息接收

- (void)viewDidLoad {

    [super viewDidLoad];

    //註冊通知

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

 

}

- (void)tongzhi:(NSNotification *)text{

    NSLog(@"%@",text.userInfo[@"textOne"]);

        NSLog(@"-----接收到通知------");

 

}

移除通知:removeObserver:和removeObserver:name:object:

其中,removeObserver:是刪除通知中心保存的調度表一個觀察者的所有入口,而removeObserver:name:object:是刪除匹配了通知中心保存的調度表中觀察者的一個入口。

這個比較簡單,直接調用該方法就行。例如:

[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];

注意參數notificationObserver爲要刪除的觀察者,一定不能置爲nil。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章