您的位置:首页 > 其它

KVO通知中心

2016-01-13 08:34 246 查看
//KVO key-value-Observer 键值对观察者

//它主要功能就是用来监听,属性里的内容发生的变化

//监听的对象,必须是观察者的属性

//参数二:要监听的属性,要和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;
// }
//

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: