麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS NSNotificationCenter通知中心使用小結

2019-10-21 18:39:15
字體:
來源:轉載
供稿:網友

前言

最近公司組織兩個星期的新人培訓,事情安排的滿滿的,周末都沒有。說好的一個星期一更新的博客中斷了,讓大家久等了,現在培訓結束,終于又可以安安靜靜的做一個程序員了,好開心。。。

一、NSNotification和Delegate的聯系和區別

眾所周知,IOS中經常會使用到NSNotification和delegate來進行一些類之間的消息傳遞。言歸正傳,這兩種有什么區別呢? 
NSNotification就是IOS提供的一個消息中心,由一個全局的defaultNotification管理應用中的消息機制。通過公開的API可以看出,這里面使用了是一個觀察者,通過注冊addObserver和解除注冊removeObserver來實現消息傳遞。蘋果文檔特別提出,在類析構的時候,要記得把removeObserver,不然就會引發崩潰,所以NSNotifcation的使用是沒有retain+1的,NSNotification是一對多的。 

至于Delegate,很簡單,就是通過增加一個指針,然后把需要調用的函數通過delegate傳遞到其他類中,來得很直截了當。不需要通過廣播的形式去實現,但是,delegate的形式只能是一對一,不能實現一對多。

在什么情況下使用Delegate和NSNotifiation呢? 

從效率上看Delegate是一個很輕量級的,相對delegate,NSNotification卻是一個很重量級的,效率上delegate明顯要比Noticication高。一般情況我們會這樣使用。 

場景一: 

A擁有B,然后B中的一些操作需要回調到A中,這時候就簡單的通過delegate回調到A。因為B是A創建的,B可以很直接的把delegate賦值A。 

場景二: 

A和B是兩個不相干的關系,A不知道B,B也不知道A,那么這時候如果通過delegate就沒辦法做到,會相對復雜。所以可以通過NSNotifcation去做一些消息傳遞。 

所以使用delegate的情況是兩者有直接的關系,至于一方知道另一方的存在。而NSNotifcation一般是大家不知道對方的存在,一般是使用跨模塊的時候使用。在使用的時候,使用delegate可能需要多寫一些delegate去實現,代碼量比較多。NSNotication只要定義相關的NotificationName就可以很方便的溝通。兩者各有所長。

二、監聽系統自帶的NSNotification

系統里定義了許多的 XxxNotification 名稱,其實只要 Cmd+Shift+O 打開 Open Quickly,輸入 NSNotification 或者 UINotification 可以看到許多以 Notification 結尾的變量定義,由變量名稱也能理解在什么時候會激發什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。

iOS,NSNotificationCenter,通知中心

使用步驟

第一步:注冊系統監聽事件

 //在NSNotificationCenter中注冊鍵盤彈出事件  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardUpEvent:) name:UIKeyboardDidShowNotification object:nil];  //在NSNotificationCenter中注冊鍵盤隱藏事件  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownEvent:) name:UIKeyboardDidHideNotification object:nil];  //在NSNotificationCenter中注冊程序從后臺喚醒事件  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

第二步:事件觸發后的處理

/** * 彈出鍵盤事件觸發處理 * * @param notification */-(void)keyboardUpEvent : (NSNotification *)notification{  //NSLog(@"鍵盤彈出事件觸發==%@",notification);  NSLog(@"鍵盤彈出事件觸發");}/** * 鍵盤隱藏事件觸發處理 * * @param notification */-(void)keyboardDownEvent : (NSNotification *)notification{  //NSLog(@"鍵盤隱藏事件觸發==%@",notification);  NSLog(@"鍵盤隱藏事件觸發");}/** * 程序從后臺喚醒觸發處理 * * @param notification */-(void)becomeActive: (NSNotification *)notification{  NSLog(@"程序從后臺喚醒觸發處理");}

第三步、在dealloc中解除監聽

/** *NSNotificationCenter 注意點:每一次在接受者對象中需要delleac把它銷毀掉。 */-(void)dealloc{  [[NSNotificationCenter defaultCenter] removeObserver:self];}

三、自定義NSNotification

這里我使用的一個實例為:在ViewController中定義一個按鈕,點擊該按鈕,同時改變兩個自定義View中的內容。

使用步驟

第一步、在ViewController中生成一個按鈕,兩個自定義View

 UIButton *postMsgBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 40)];  [postMsgBtn setTitle:@"發送消息" forState:UIControlStateNormal];  postMsgBtn.backgroundColor = [UIColor grayColor];  [postMsgBtn addTarget:self action:@selector(postMsg:) forControlEvents:UIControlEventTouchUpInside];  [self.view addSubview:postMsgBtn];  MyView *view = [[MyView alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];  [self.view addSubview:view];  MyView *view2 = [[MyView alloc] initWithFrame:CGRectMake(50, 320, 100, 50)];  [self.view addSubview:view2];

第二步、點擊按鈕,發送Notification

-(void)postMsg: (UIButton *)btn{  [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_NAME object:nil userInfo:@{@"msg":@"jingming1"}];}

第三步、在自定義View中注冊監聽事件

 

復制代碼代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:NOTIFICATION_MESSAGE_NAME object:nil];

 

第四步、處理監聽事件

-(void)acceptMsg : (NSNotification *)notification{  NSLog(@"%@",notification);  NSDictionary *userInfo = notification.userInfo;  _label.text = [userInfo objectForKey:@"msg"];}

第五步、在dealloc中解除監聽

-(void)dealloc{  [[NSNotificationCenter defaultCenter] removeObserver:self];}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品久久久久久久久久久久久久 | 吾色视频 | 中文字幕爱爱视频 | 一区二区精品视频在线观看 | 日韩黄色一级视频 | 免费a观看| 深夜影院a| 午夜视频大全 | 久久一级 | 一级电影免费在线观看 | 国产69精品久久久久久久久久 | 久久国产一级片 | 日日噜噜噜噜久久久精品毛片 | 国产宾馆3p国语对白 | 欧美日韩一 | 日本爽快片100色毛片视频 | 午夜视频亚洲 | 91短视频在线观看 | av电影免费观看 | 国产精品亚洲一区二区三区在线观看 | 午夜视频久久久 | 91小视频在线观看免费版高清 | 在线免费观看精品 | 国产精品成人一区二区三区电影毛片 | 日韩精品久久久久久 | 国产精品高清一区 | 日韩视频一二三 | 久久一本日日摸夜夜添 | 日韩毛片一区二区三区 | 久久久一区二区精品 | 亚洲日本韩国在线观看 | 精品一区二区三区中文字幕老牛 | 91精品国产乱码久久久久 | 亚洲精品动漫在线观看 | 国产精品免费观在线 | 国产精品91在线 | 国产亚洲精彩视频 | 91精品国产91久久久 | 久久午夜国产 | 中文字幕亚洲视频 | 国产亚洲精品久久久久5区 男人天堂免费 |