天天看點

IOS開發基礎篇--NSNotificationCenter使用小結

前言

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

一、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];
}           

四、參考文獻

本來想自己總結文字寫的,鑒于自己時間有限,而且網上有些部落格總結也挺不錯的,跟自己的想法很符合。是以有些就把别人的知識成果搬過來了,在次聲明一下。

http://m.blog.csdn.net/blog/hzhenx1989/20217387

http://www.cnblogs.com/wenxp2006/articles/2499330.html

五、部落客的話

以前看過很多别人的部落格,學到不少東西。現在準備自己也開始寫寫部落格,希望能夠幫到一些人。

聯系方式:

微網誌:新浪微網誌

部落格:http://blog.csdn.net/yixiangboy

github:https://github.com/yixiangboy