KVO通知中心

//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;
//    }
//    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值