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 {
self.stu = [[Student alloc] init];
[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];
[[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];
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的内容,编辑完成.
[[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];
}