UI17_KVO, 通知中心

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

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)dealloc
{
    //  ARC下也可以写dealloc方法, 但是不能写[super dealloc], 它需要把观察者从对象上移除掉
    [self.stu removeObserver:self forKeyPath:@"name"];
    [_stu release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//    //  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 = @"韩老师";

//    //  传值的第四种方式: 通知中心也叫消息中心
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.view addSubview:button];
//    button.frame = CGRectMake(100, 100, 150, 50);
//    button.backgroundColor = [UIColor orangeColor];
//    [button setTitle:@"前往" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    
//    //  通过通知中心, 先给它添加一个观察者
//    //  参数1: 添加一个观察者
//    //  参数2: 需要绑定一个方法
//    //  参数3: 给name起一个名
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backValue:) name:@"test" object:nil];

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
    [self.view addSubview:self.textField];
    [_textField release];
    self.textField.backgroundColor = [UIColor cyanColor];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.cornerRadius = 10;
    //  第一种监听textField输入内容的方法
//    [self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];

    //  第二种: 通过通知中心监听textField的内容
    //  参数3: 在textField头文件的最后几行
    //  参数4: 要监听的对象
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(value:) name:UITextFieldTextDidChangeNotification object:self.textField];


}
- (void)value:(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];
    if ([cate evaluateWithObject:self.textField.text]) {
        NSLog(@"符合");
    } else {
        NSLog(@"不符合");
    }

}

- (void)changeValue:(UITextField *)textField {
    NSLog(@"%@", textField.text);
}

- (void)backValue:(NSNotification *)notification {
    NSLog(@"%@", notification.userInfo);
}

- (void)click:(UIButton *)button {
    SecondViewController *secVC = [[SecondViewController alloc] init];
    [self presentViewController:secVC animated:YES completion:^{

    }];
}

//  触发的方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@", change[@"new"]);
    NSLog(@"%@", keyPath);
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    button.frame = CGRectMake(100, 200, 150, 50);
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)click:(UIButton *)button {
    //  把值进行返回
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"1":@"2"}];


    [self dismissViewControllerAnimated:YES completion:^{

    }];
}

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *hobby;

@end

Student.m

#import "Student.h"

@implementation Student
- (void)dealloc
{
    [_name release];
    [_hobby release];
    [super dealloc];
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值