KVO和Notification
来源:互联网 发布:mysql 5.5.37 for mac 编辑:程序博客网 时间:2024/06/11 05:04
实现功能 监控UITextField是否有输入文本 从而实现按钮是否可以点击
解决方案 Notification
代码实现
01
//文本框
02
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
03
textField.tag = 888;
04
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
05
[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
06
textField.layer.borderWidth = 0.5f;
07
//添加文本改变通知
08
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:textField];
09
[self.view addSubview:textField];
10
11
//文本框内容改变时触发
12
- (
void
) textChanged:(NSNotification *) notification
13
{
14
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@
"提示"
message:[NSString stringWithFormat:@
"%@"
,notification.object] delegate:nil cancelButtonTitle:@
"确定"
otherButtonTitles:nil];
15
[alertView show];
16
}
由此引发的一些思考,想详细整理下 KVO 和 Notification 的使用
KVO
1.什么是KVO
KVO 即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。
简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
2.实现步骤:
1)注册(指定被观察者的属性)
2)实现回调方法
3)移除观察
3.代码实现
01
//文本框
02
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
03
textField.tag = 888;
04
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
05
[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
06
//1.注册
07
[textField addObserver:self forKeyPath:@
"text"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
08
textField.layer.borderWidth = 0.5f;
09
[self.view addSubview:textField];
10
11
//2.实现回调方法
12
- (
void
) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(
void
*)context
13
{
14
if
([keyPath isEqualToString:@
"text"
]) {
15
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@
"提示"
message:[NSString stringWithFormat:@
"%@"
,object] delegate:nil cancelButtonTitle:@
"确定"
otherButtonTitles:nil];
16
[alertView show];
17
}
18
}
19
20
21
//3.移除通知
22
UITextField *textField = (UITextField *)[self.view viewWithTag:888];
23
[textField removeObserver:self forKeyPath:@
"text"
];
Notification
作用: NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.
2.实现步骤:
1)注册(在什么地方接收消息)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"TestNotification" object:nil];
2)发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:myObject];
注:postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知。
object:传递的参数
3)注册方法的写法:
- (void) test:(NSNotification*) notification
{
id obj = [notification object];//获取到传递的参数 即上方的myObject
}
4)移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"TestNotification" object:nil];
- KVO和Notification
- delegate、notification和KVO
- Notification和KVO有什么不同
- IOS中delegate、notification和KVO的区别和介绍
- 8观察者模式( kvo基于键值 和Notification通知)
- delegate, notification和kvo模式实现通信的优缺点
- iOS开发:Delegate、Notification和KVO的联系与区别
- 观察者模式( kvo基于键值 和Notification通知)
- Delegate,Notification,KVO
- Delegate,Notification,KVO
- Delegate,Notification,KVO
- delegate,notification,kvo比较
- KVO与Notification区别
- Notification、KVO实现代码
- Delegate, Notification,KVO优缺点
- Delegate Notification KVO 整理
- delegate , Notification , protocal , KVC , KVO
- 如何选择delegate、notification、KVO
- 工业相机知识
- oracle 断开当前用户
- AFNetwork网路请求 队列代码
- Word Search:from LeetCode
- 史上最全的maven pom.xml文件教程详解
- KVO和Notification
- iOS开发-自定义后台显示图片(iOS7-Background Fetch的应用)
- python字典构造函数dict(mapping)解析
- java的一些基本概念
- 帆软与用友:共建安徽国资委财务信息系统
- [leetcode]Minimum Window Substring
- POJ 1047——解题报告
- vs如何向main函数传参数、设置字符编码、设置OpenMp等
- 使用lint提高android代码质量