UIAlertController KVO
2015-08-25 17:48
429 查看
- (void)viewDidLoad {
[super viewDidLoad];
self.stu=[[Student alloc] init];//kvc Key-Value-Coding
//kvo Key-Value_Obsever 键值观察者
//可以监控对象里的属性值变化,只有值发生了变化就会触发方法
//监听属性的值得变化,一定要用设置器,否则监听失效
//注册一个监听
[self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:@"这是监控的文本"];
self.stu.name=@"张三";
//创建一个button,然后能push到下一页
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor=[UIColor redColor];
button.frame=CGRectMake(100, 100, 100, 100);
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//传值的第四种方法,通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receive:) name:@"test" object:nil];
self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 300, 200, 40];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=10;
[self.view addSubview:self.textField];
[_textField release];
//通过中心监听输入框的内容
//第一个参数:添加监听者的对象,一般都是当前文件的对象,也就是self
//第二个参数:监听触发方法
//第三个参数:监听的名,如果对textfield进行监听,UITextField提供了专门的常量字符串,127行
//第四个参数:要把监听的对象放到最后一位,如果只用于传值,则是nil
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValues:) name:UITextFieldTextDidChangeNotification object:self.textField];}
//通过中心把值带回到之前设置的方法里
-(void)receive:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
}
//只要值变化就会触发下面的方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{//只要监听的属性内容发生了改变,就会马上触发这个方法
NSLog(@"%@",change);}
-(void)changeValues:(NSNotification *)notification{
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];
//然后进行判断,返回一个BOOL类型的结果
BOOL result=[cate evaluateWithObject:self.textField.text];
if(result){NSLog(@"吻合");
}else{NSLog(@"不吻合");
}}
-(void)click:(UIButton *)button{
SeaendViewController *secend=[[SeaendViewController alloc] init];
[self.navigationController pushViewController:secend animated:YES];
[secend release];
UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"提示" message:@"小王子" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertVC animated:YES completion:^{
}];
//给它添加textfield
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder=@"请输入内容";
//把监听写在textField添加的方法里
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTesxt:) name:UITextFieldTextDidChangeNotification object:textField];
}];
//添加俩个按钮
UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//在里面写点击方法
UITextField *text=alertVC.textFields[0];
NSLog(@"%@",text.text);
}];
[alertVC addAction:sureAction];
//设置按钮默认是用不了
sureAction.enabled=NO;}
-(void)changeTesxt:(NSNotification *)notification{//找到当前的UIAlterController
UIAlertController *alterVC=(UIAlertController *)self.presentedViewController;
UITextField *textField=alterVC.textFields[0];
UIAlertAction *action=alterVC.actions[0];
action.enabled=textField.text.length>5?YES:NO;}
-(void)dealloc{
//如果添加了观察者,就要在dealloc方法里把对应的观察者移除,如果不移除的话,可能会造成内存上的问题
//在arc中也可以写dealloc方法,主要就是为了移除观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.stu removeObserver:self forKeyPath:@"name"];
[_stu release];
[_textField release];
[super dealloc];
}
[super viewDidLoad];
self.stu=[[Student alloc] init];//kvc Key-Value-Coding
//kvo Key-Value_Obsever 键值观察者
//可以监控对象里的属性值变化,只有值发生了变化就会触发方法
//监听属性的值得变化,一定要用设置器,否则监听失效
//注册一个监听
[self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:@"这是监控的文本"];
self.stu.name=@"张三";
//创建一个button,然后能push到下一页
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor=[UIColor redColor];
button.frame=CGRectMake(100, 100, 100, 100);
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//传值的第四种方法,通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receive:) name:@"test" object:nil];
self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 300, 200, 40];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=10;
[self.view addSubview:self.textField];
[_textField release];
//通过中心监听输入框的内容
//第一个参数:添加监听者的对象,一般都是当前文件的对象,也就是self
//第二个参数:监听触发方法
//第三个参数:监听的名,如果对textfield进行监听,UITextField提供了专门的常量字符串,127行
//第四个参数:要把监听的对象放到最后一位,如果只用于传值,则是nil
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValues:) name:UITextFieldTextDidChangeNotification object:self.textField];}
//通过中心把值带回到之前设置的方法里
-(void)receive:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
}
//只要值变化就会触发下面的方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{//只要监听的属性内容发生了改变,就会马上触发这个方法
NSLog(@"%@",change);}
-(void)changeValues:(NSNotification *)notification{
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];
//然后进行判断,返回一个BOOL类型的结果
BOOL result=[cate evaluateWithObject:self.textField.text];
if(result){NSLog(@"吻合");
}else{NSLog(@"不吻合");
}}
-(void)click:(UIButton *)button{
SeaendViewController *secend=[[SeaendViewController alloc] init];
[self.navigationController pushViewController:secend animated:YES];
[secend release];
UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"提示" message:@"小王子" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertVC animated:YES completion:^{
}];
//给它添加textfield
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder=@"请输入内容";
//把监听写在textField添加的方法里
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTesxt:) name:UITextFieldTextDidChangeNotification object:textField];
}];
//添加俩个按钮
UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//在里面写点击方法
UITextField *text=alertVC.textFields[0];
NSLog(@"%@",text.text);
}];
[alertVC addAction:sureAction];
//设置按钮默认是用不了
sureAction.enabled=NO;}
-(void)changeTesxt:(NSNotification *)notification{//找到当前的UIAlterController
UIAlertController *alterVC=(UIAlertController *)self.presentedViewController;
UITextField *textField=alterVC.textFields[0];
UIAlertAction *action=alterVC.actions[0];
action.enabled=textField.text.length>5?YES:NO;}
-(void)dealloc{
//如果添加了观察者,就要在dealloc方法里把对应的观察者移除,如果不移除的话,可能会造成内存上的问题
//在arc中也可以写dealloc方法,主要就是为了移除观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.stu removeObserver:self forKeyPath:@"name"];
[_stu release];
[_textField release];
[super dealloc];
}
相关文章推荐
- Android UI设计——ListView控件与SimpleAdapter适配器(三)
- 【Material Design视觉设计语言】UI组件设计(一):按钮
- leetcode:Permutation Sequence
- 【傻傻分不清楚】四 Request.Form 和 Request.QueryString
- UIActionViewController 详解 iOS8
- hdu 5412 CRB and Queries(动态区间第k大值,区间能修改)(整体二分,树状数组套平衡树,线段树套treap)
- 【记忆化DFS】HDOJ1242 Rescue
- setLatestEventInfo(),Builder.getNotification()过时,Android提供的解决方案
- C# Winform跨线程更新UIkongjian
- iOS图片填充UIImageView(contentMode)
- 关于UINavigationController配置导航条
- The influence of depreciation on plywood export
- oldssoj2672Y sequence(mobius函数容斥+迭代)
- easyui datagrid
- C# 跨线程操作UI元素
- 关于UITabBar配置
- java生成UUID通用唯一识别码 (Universally Unique Identifier)
- UIButton文字居左显示
- Solr: index product and price for sellers and perfoming query and sorting
- zoj 1292 Integer Inquiry【大数】