細說KVO和Notification

    作iOS開發已經不短期了,忙於項目,碰見問題,就去Google,忘記了積累,最近深知其中的危害,深深體會到經驗不在於年限而在於積累這個道理。post

    從碰見的問題提及,想實現時刻監控UITextField有無文本這樣一個功能,聽起來很簡單,感受作起來也很容易,但就是這麼個簡單的問題,讓我花費了很長時間,非常懊惱,決定從新審視下本身,整理下這方面的知識,來提升本身!好了廢話少說,進入正題!
spa

    實現功能    監控UITextField是否有輸入文本 從而實現按鈕是否能夠點擊
code

    解決方案     Notificationorm

    代碼實現server

    //文本框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
    textField.tag = 888;
    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    textField.layer.borderWidth = 0.5f;
    //添加文本改變通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:textField];
    [self.view addSubview:textField];
    
    //文本框內容改變時觸發
- (void) textChanged:(NSNotification *) notification
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",notification.object] delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
    [alertView show];
}


由此引起的一些思考,想詳細整理下 KVO 和 Notification 的使用對象

KVO開發

    1.什麼是KVOrem

        KVO 即:Key-Value Observing,它提供一種機制,當指定的對象的屬性被修改後,則對象就會接受到通知。編譯器

    簡單的說就是每次指定的被觀察的對象的屬性被修改後,KVO就會自動通知相應的觀察者了。string

    2.實現步驟:

        1)註冊(指定被觀察者的屬性)

        2)實現回調方法

        3)移除觀察

    3.代碼實現

   //文本框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
    textField.tag = 888;
    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    //1.註冊
    [textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    textField.layer.borderWidth = 0.5f;
    [self.view addSubview:textField];
    
//2.實現回調方法
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"text"]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",object] delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
        [alertView show];
    }
}


        //3.移除通知
        UITextField *textField = (UITextField *)[self.view viewWithTag:888];
        [textField removeObserver:self forKeyPath:@"text"];

Notification

     做用: NSNotificationCenter是專門供程序中不一樣類間的消息通訊而設置的.

    2.實現步驟:

        1)註冊(在什麼地方接收消息)

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

       2)發送通知

            [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"                                            object:myObject];

            注:postNotificationName:通知的名字,也是通知的惟一標示,編譯器就經過這個找到通知。

                 object:傳遞的參數

         3)註冊方法的寫法:

            - (void) test:(NSNotification*) notification

            {

               id obj = [notification object];//獲取到傳遞的參數 即上方的myObject

             } 

         4)移除通知

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

相關文章
相關標籤/搜索