KVO

这里写图片描述

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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值