iOS開發-訊息通知機制(NSNotification和NSNotificationCenter)

來源:互聯網
上載者:User

標籤:

iOS中委託模式和訊息機制基本上開發中用到的比較多,一般最開始頁面傳值通過委託實現的比較多,類之間的傳值用到的比較多,不過委託相對來說只能是一對一,比如說頁面A跳轉到頁面B,頁面的B的值改變要映射到頁面A,頁面C的值改變也需要映射到頁面A,那麼就需要需要兩個委託解決問題。NSNotificaiton則是一對多註冊一個通知,之後回調很容易解決以上的問題。

基礎概念 iOS訊息通知機制算是同步的,觀察者只要向訊息中心註冊, 即可接受其他對象發送來的訊息,訊息寄件者和訊息接受者兩者可以互相一無所知,完全解耦。這種訊息通知機制可以應用於任意時間和任何對象,觀察者可以有多個,所以訊息具有廣播的性質,只是需要注意的是,觀察者向訊息中心註冊以後,在不需要接受訊息時需要向訊息中心登出,屬於典型的觀察者模式。

訊息通知中重要的兩個類:

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

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

實戰演練

1.通過NSNotificationCenter註冊通知NSNotification,viewDidLoad中代碼如下:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFirst:) name:@"First" object:nil];        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSecond:) name:@"Second" object:nil];

第一個參數是觀察者為本身,第二個參數表示訊息回調的方法,第三個訊息通知的名字,第四個為nil表示表示接受所有寄件者的訊息~

回調方法:

-(void)notificationFirst:(NSNotification *)notification{    NSString  *name=[notification name];    NSString  *object=[notification object];    NSLog(@"名稱:%@----對象:%@",name,object);}-(void)notificationSecond:(NSNotification *)notification{    NSString  *name=[notification name];    NSString  *object=[notification object];    NSDictionary  *dict=[notification userInfo];    NSLog(@"名稱:%@----對象:%@",name,object);    NSLog(@"擷取的值:%@",[dict objectForKey:@"key"]);}

2.訊息傳遞給觀察者:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:@"部落格園-Fly_Elephant"];        NSDictionary  *dict=[[NSDictionary alloc]initWithObjects:@[@"keso"] forKeys:@[@"key"]];        [[NSNotificationCenter defaultCenter] postNotificationName:@"Second" object:@"http://www.cnblogs.com/xiaofeixiang" userInfo:dict];

3.頁面跳轉:

 

-(void)pushController:(UIButton *)sender{    ViewController *customController=[[ViewController alloc]init];    [self.navigationController pushViewController:customController animated:YES];}

4.銷毀觀察者

-(void)dealloc{    NSLog(@"觀察者銷毀了");    [[NSNotificationCenter defaultCenter] removeObserver:self];}

 也可以通過name單個刪除:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"First" object:nil];

5.運行結果

2015-04-26 15:08:25.900 CustoAlterView[2169:148380] 觀察者銷毀了2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名稱:First----對象:部落格園-Fly_Elephant2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名稱:Second----對象:http://www.cnblogs.com/xiaofeixiang2015-04-26 15:08:29.223 CustoAlterView[2169:148380] 擷取的值:keso

 

iOS開發-訊息通知機制(NSNotification和NSNotificationCenter)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.