KVO
2015-10-19 21:54
225 查看
ViewController.m #import "ViewController.h" #import "Student.h" #import "SecondViewController.h" @interface ViewController () @property(nonatomic, retain)Student *stu; @property(nonatomic, retain)UITextField *textField; @end @implementation ViewController - (void)viewDidLoad { // KVC Key - Value - Coding // KVO Key - Value - Observer 键值观察者.监控属性的值得变化,只要变化就会触发相应的操作. self.stu = [[Student alloc] init]; // 注册一个观察者 // 参数1:给对象添加一个观察者,就是当前类的对象self. // 参数2:需要监控的属性名. // 参数3:触发的条件,旧内容变成新内容就会触发. [self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"监控的文本"]; self.stu.name = @"韩老师";
用此方法来监控目标对象. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@", change[@"new"]); NSLog(@"%@", keyPath); }
1.传值的第四种方式:通知中心也叫消息中心. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 100, 100, 50); button.backgroundColor = [UIColor yellowColor]; [button setTitle:@"前往" forState:UIControlStateNormal]; [button addTarget:self action:@selector(goClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; // 给通知中心加上一个观察者. // 参数1:添加一个观察者. // 参数2:需要绑定一个方法. // 参数3:给name起一个名. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backValue:) name:@"test" object:nil];
按钮绑定的方法. - (void)goClick:(UIButton *)button { SecondViewController *secondVC = [[SecondViewController alloc] init]; [secondVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; [self presentViewController:secondVC animated:YES completion:^{ }]; [secondVC release]; } 打印通知中心传回来的字典. - (void)backValue:(NSNotification *)notification { NSLog(@"%@", notification.userInfo); }
SecondViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor cyanColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 100, 100, 50); button.backgroundColor = [UIColor greenColor]; [button setTitle:@"返回" forState:UIControlStateNormal]; [button addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)backClick:(UIButton *)button { // 把值进行返回. [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"1":@"2"}]; [self dismissViewControllerAnimated:YES completion:^{ }]; }
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; self.textField.backgroundColor = [UIColor greenColor]; [self.view addSubview:self.textField]; [_textField release]; self.textField.layer.borderWidth = 1; self.textField.layer.cornerRadius = 10; 2.第一种:监听textField输入内容的方法. [self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
监控文本框的输入内容,正在编辑. - (void)changeValue:(UITextField *)textField { NSLog(@"%@", textField.text); }
3.第二种:通过通知中心监听textField的内容,编辑完成. // 参数3:在textField头文件的最后几行 // 参数4:要监听的对象 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(value:) name:UITextFieldTextDidChangeNotification object:self.textField];
编辑完成的方法,用正则表达式和谓词. - (void)value:(NSNotification *)natification { NSLog(@"%@", self.textField.text); // 这个正则表达式是用来判断一个内容格式是否相符的字符串,正则表达式有自己的语法规则,需要的话,百度. NSString *str = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; // 通过谓词来进行内容的比对. NSPredicate *cate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str]; if ([cate evaluateWithObject:self.textField.text]) { NSLog(@"YES"); } else { NSLog(@"NO"); } }
- (void)dealloc { // ARC下可以写dealloc方法,但是不能写[super dealloc],它需要把观察者从对象上移除掉 [self.stu removeObserver:self forKeyPath:@"name"]; [_stu release]; [super dealloc]; }
相关文章推荐
- 日常总结(五)同时启动myeclipse和eclipse如何解决端口被占用
- 1019. General Palindromic Number
- Android五大布局之相对布局
- 各种标签
- Android数据存储(2):Internal Storage
- 201510192043_《Javascript权威指南(第六版)——ES5中的类、保存对象状态》(P239-243)
- HIVE入门_1
- linux下导入、导出mysql数据库文件的命令
- 阶乘计算升级版
- OpenGL编程学习实战教程
- 杭电(1269)迷宫城堡(强联通之定义算法)
- Android 5.0(Lollipop)事件输入系统(Input System)
- Scrum敏捷开发
- urI转码
- java web(发送邮件,以及要整合的包)
- SSH框架搭建登录实例
- Linux多线程基础学习(五)线程同步-同步概念、互斥锁
- JSON详解
- qtxlsx将excel中的数据读取到widget上
- 关于UFT的一些小想法