UI28_KVO(key-value-observer键值观察者) 通知中心

来源:互联网 发布:java重定向跳转 编辑:程序博客网 时间:2024/06/05 21:03

1.移除通知中心

[[NSNotificationCenter defaultCenter]removeObserver:self];

2.KVO的简述

监控对象里的属性值需变化,只有值发生变化就会触发这个方法.监听属性的值的变化一定要用设置器,否则监听无效.给这个对象注册一个监听者告诉监听者当前是哪个类对应的对象

3.使用过程

(1)注册,指定被观察的属性(2)实现回调的方法(3)移除观察
 self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];    self.textFiled.layer.borderWidth = 1;    self.textFiled.layer.cornerRadius = 10;    [self.view addSubview:self.textFiled];    [_textFiled release];

第一种监听方法

[self.textFiled addTarget:self action:@selector(text:) forControlEvents:UIControlEventEditingChanged];- (void)text:(UIButton *)button{    NSLog(@"这是text");}

第二种通过中心监听输入框的内容

添加观察者还是当前的类,捆绑一个方法,要用它去监听textFiled//第一个参数:添加监听者对象,一般都是当前文件的对象也就是self//第二个参数:监听触发的方法//第三个参数:监听的名,如果对textField进行监听,UITextFileld提供了专门的常量字符串,127行//第四个参数:把要监听的对象放到最后一位,如果只用于传值则是nil [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeValue:) name:UITextFieldTextDidChangeNotification object:self.textFiled]; 值变触发的方法 //值变就走此方法- (void)changeValue:(NSNotification *)notification{    NSLog(@"%@",self.textFiled.text);    //这个是用来判断电话号码的正则表达式,正则表达式通过自己的语法规则可以用一串表达式判断身份证号等信息,如果需要,就百度    NSString *str = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";    //用谓词来判断当前输入的文本框的内容和正则表达式格式是否吻合    NSPredicate *cate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",str];    //然后进行判断,返回一个BOOL类型的结果    BOOL result = [cate evaluateWithObject:self.textFiled.text];    if (result) {        NSLog(@"符合条件");    }else    {        NSLog(@"不符合条件");    }}   

点击button出现对话框 当textfield的长度大于多少可以点击确认

//创建一个Button,然后能push到下一页    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.frame = CGRectMake(30, 30, 100, 100);    [button setTitle:@"下一页" forState:UIControlStateNormal];    [button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside ];    [self.view addSubview:button];触发的方法- (void)pushAction:(UIButton *)button{    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"张阳阳" preferredStyle:UIAlertControllerStyleAlert];    [self presentViewController:alertVC animated:YES completion:nil];    //给他添加textField    [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {        //占位符placeholder        textField.placeholder = @"请输入内容";        //把监听写在textField添加的方法里        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];    }];    //添加两个按钮    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        //在里面写点击方法        UITextField *textField = alertVC.textFields[0];        NSLog(@"%@",textField.text);    }];    [alertVC addAction:sureAction];    //设置让按钮默认用不了    sureAction.enabled = NO;}触发方法里的触发方法- (void)changeText:(NSNotification *)notification{    UIAlertController *alertVC = (UIAlertController *)self.presentedViewController;    //找到textFiel    UITextField *textField = alertVC.textFields[0];    //找到Action设置enable    UIAlertAction *action = alertVC.actions[0];    action.enabled = textField.text.length > 5;}

通知中心传值后往前传值

流程1button点击进入下一页- (void)pushAction:(UIButton *)button{    SecondViewController *second = [[SecondViewController alloc] init];    [self.navigationController pushViewController:second animated:YES];    [second release];}流程2跳转第二页- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.frame = CGRectMake(30, 30, 100, 100);    [button setTitle:@"返回" forState:UIControlStateNormal];    [button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside ];    [self.view addSubview:button];}- (void)pushAction:(UIButton *)button{    [self.navigationController popViewControllerAnimated:YES];    //通过控制中心进行反方向传值    //通知中心是通过一个字典来传值的    NSDictionary *dic = @{@"dalfka":@"1",@"daff":@"2"};    [[NSNotificationCenter defaultCenter]postNotificationName:@"text" object:nil userInfo:dic];}流程3传值第四种方法.通知中心    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(acceptAction:) name:@"text" object:nil];流程4//通过中心把值带回到之前设置的方法里- (void)acceptAction:(NSNotification *)notification{    NSLog(@"%@",notification.userInfo);}

监听类的属性

self.stu.name = @"张三";[self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"这是监控的文本"];//只要值变化就会触发下面的方法- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    //只要监听的属性内容发生了变化,就会马上触发这个方法    NSLog(@"%@",change);}

释放(增加观察与取消观察是成对出现的,所以需要在最后的时候,移除观察者)

- (void)dealloc{    //如果添加了观察者,就要在dealloc方法里把对应的观察者移除,如果不移除的话,可能造成内存上的问题    //arc中也能写dealloc而且它中间就有移除观察者的方法    [[NSNotificationCenter defaultCenter]removeObserver:self];    [self.stu removeObserver:self forKeyPath:@"name"];}

相关解释

调用方法是:object : 被观察对象observer: 观察对象forKeyPath里面带上property的name,如UIView的frame、center等等options:4个值,分别是:NSKeyValueObservingOptionNew 把更改之前的值提供给处理方法NSKeyValueObservingOptionOld 把更改之后的值提供给处理方法NSKeyValueObservingOptionInitial 把初始化的值提供给处理方法,一旦注册,立马就会调用一次。通常它会带有新值,而不会带有旧值。NSKeyValueObservingOptionPrior 分2次调用。在值改变之前和值改变之后。注:例子里的0就代表不带任何参数进去context: 可以带入一些参数,任何类型都可以,强制转就可以。

注册

-(void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void*)contextkeyPath就是要观察的属性值,options给你观察键值变化的选择,而context方便传输你需要的数据(注意这是一个void型)。options是监听的选项,也就是说明监听泛黄的额字典包含什么值。有两个常用的选项:NSKeyValueObservingOptionsNew 指返回的字典包含新值。NSKeyValueObservingOptionsOld 值返回的字典包含旧值。

接触注册

- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
0 0
原创粉丝点击