KVO通知中心
2016-01-13 08:34
246 查看
//KVO key-value-Observer 键值对观察者
//它主要功能就是用来监听,属性里的内容发生的变化
//监听的对象,必须是观察者的属性
//参数二:要监听的属性,要和keypath保持一致,否则不会触发方法
//参数三:触发的条件,就是旧值和新值变化的时候
//它主要功能就是用来监听,属性里的内容发生的变化
//监听的对象,必须是观察者的属性
//参数二:要监听的属性,要和keypath保持一致,否则不会触发方法
//参数三:触发的条件,就是旧值和新值变化的时候
// self.stu=[[Student alloc] init]; // [self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"nihao"]; // self.stu.name=@"张三";
#import "ViewController.h"
#import "Student.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
- (IBAction)myButton:(id)sender;
@property(nonatomic,retain)Student *stu;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//KVO key-value-Observer 键值对观察者
//它主要功能就是用来监听,属性里的内容发生的变化
//监听的对象,必须是观察者的属性
//参数二:要监听的属性,要和keypath保持一致,否则不会触发方法
//参数三:触发的条件,就是旧值和新值变化的时候
// self.stu=[[Student alloc] init]; // [self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"nihao"]; // self.stu.name=@"张三";
//通知中心
// //第一种使用:传值
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"chengjinle" object:nil];
//第二种使用:监听文本框输入的内容
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldValue:) name:UITextFieldTextDidChangeNotification object:self.myTextField];
}
//第二种使用
-(void)textFieldValue:(NSNotification *)notifiaction{
NSLog(@"%@",self.myTextField.text);
//正则表达式
NSString *str=@"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//通过谓词来判断,当前输入的内容是否符合要求,这个要求可以通过正则表达式来实现
NSPredicate *cate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
BOOL result=[cate evaluateWithObject:self.myTextField.text];
if (result) {
NSLog(@"电话号码成立");
}else{
NSLog(@"不成立");
}
}
//第一种使用
-(void)change:(NSNotification *)notigication{
NSLog(@"%@",notigication.userInfo);
}
//kvo
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"keypath=%@",keyPath);
NSLog(@"change=%@",change);
NSLog(@"context=%@",context);
}
//arc下也需要写dealloc
-(void)dealloc{
//dealloc方法里写移除观察者
[self.stu removeObserver:self forKeyPath:@"name"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)myButton:(id)sender {
// SecondViewController *secVC=[[SecondViewController alloc] init];
// [secVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
// [self presentViewController:secVC animated:YES completion:^{
//
// }];
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"程金乐" message:@"尹德建" preferredStyle:UIAlertControllerStyleAlert];
//添加textField
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"你好";
[ [NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeAlertValue:) name:UITextFieldTextDidChangeNotification object:textField];
}];
//添加一个能用来点击的button
UIAlertAction *action=[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击获取textField里的内容
UITextField *text=[alert.textFields firstObject];
NSLog(@"%@",text.text);
}];
[alert addAction:action];
//取消按钮的点击效果
action.enabled=NO;
[self presentViewController:alert animated:YES completion:^{
}];
}
-(void)changeAlertValue:(NSNotification *)notification{
//先找alertController
UIAlertController *alert=(UIAlertController *)self.presentedViewController;
UITextField *textField=alert.textFields[0];
UIAlertAction *action=alert.actions[0];
//大于4按钮才可以用
action.enabled=textField.text.length>4;
// if (textField.text.length>4) {
// action.enabled=YES;
// }
//
}
相关文章推荐
- linux 命令 —— netstat
- 一边是马云,一边是星云
- ubuntu下resolv.conf文件的自动覆盖问题
- C语言实现通用数据结构的高效设计
- angular + browserify + gulp + bower + less
- 常规功能和模块自定义系统 (cfcmms)—032开发日志(用GoJS来绘制模块关系图)
- 关于android的2.2与4.4的文件读取的一点发现
- 物联网技术趋势预测
- windows安装程序无法将windows配置为在此计算机的硬件上运行
- UDP发送和接收
- 两个移动web开发在线测试工具
- 嵌入式Linux学习小结
- zabbix监控打印机
- 数据库字段命名 --工作备忘2016/1/12
- iOS开发-模板方法模式
- C#打开文件对话框
- 内存管理细节1
- Django 中 如何使用 settings.py 中的常量
- 设计模式(十):外观模式
- Wireshark系列之4 捕获过滤器