KVO和Notification

来源:互联网 发布:mysql 5.5.37 for mac 编辑:程序博客网 时间:2024/06/11 05:04

实现功能    监控UITextField是否有输入文本 从而实现按钮是否可以点击

    解决方案     Notification

    代码实现

01    //文本框
02    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
03    textField.tag = 888;
04    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
05    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
06    textField.layer.borderWidth = 0.5f;
07    //添加文本改变通知
08    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:textField];
09    [self.view addSubview:textField];
10     
11    //文本框内容改变时触发
12- (void) textChanged:(NSNotification *) notification
13{
14    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",notification.object] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
15    [alertView show];
16}


由此引发的一些思考,想详细整理下 KVO 和 Notification 的使用

KVO

    1.什么是KVO

        KVO 即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。

    简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

    2.实现步骤:

        1)注册(指定被观察者的属性)

        2)实现回调方法

        3)移除观察

    3.代码实现

01   //文本框
02    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
03    textField.tag = 888;
04    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
05    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
06    //1.注册
07    [textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
08    textField.layer.borderWidth = 0.5f;
09    [self.view addSubview:textField];
10     
11//2.实现回调方法
12- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
13{
14    if ([keyPath isEqualToString:@"text"]) {
15        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",object] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
16        [alertView show];
17    }
18}
19 
20 
21        //3.移除通知
22        UITextField *textField = (UITextField *)[self.view viewWithTag:888];
23        [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];



0 0
原创粉丝点击