KVO
来源:互联网 发布:郑州软件定制 编辑:程序博客网 时间:2024/06/11 00:51
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];}
0 0
- KVO
- kvo
- kvo
- KVO
- KVO
- Kvo
- KVO
- kvo
- KVO
- KVO
- KVO
- KVO
- KVO
- KVO
- KVO
- KVO
- KVO
- KVO
- 1019. General Palindromic Number
- 各种标签
- 同时启动myeclipse和eclipse如何解决端口被占用
- Android五大布局之相对布局
- AsyncTask源码解析
- KVO
- 雨
- Integer Long大小比较
- 阿里云 Contos 6.5 + nginx + uwsgi + django环境部署
- HDU 1272
- Android 布局优化
- 八选一数据选择器
- 关于如何将action中传给jsp的值以下拉框的形式显示出来
- mysql 存储过程详解