iOS开发中 代理,block,KVO 的使用
来源:互联网 发布:网络兼职是真是假 编辑:程序博客网 时间:2024/06/05 04:57
1.代理
IPView.h
@protocol TPViewDelegate <NSObject>
- (void)IPView:(UIView*)view finishEdit:(NSString *)text;
@end
@interface IPView : UIView <UITextFieldDelegate>
@property (nonatomic,assign)id<TPViewDelegate> delegate;
@end
IPView.m
- (void)pushMessage{
[_textView endEditing:YES];
if (str!= nil) {
[self.delegateIPView:self finishEdit:str];
}
self.hidden= YES;
}
ViewController.m
- (void)IPView:(UIView*)view finishEdit:(NSString *)text{
self.label.text= text;
}
IPView.h
@protocol TPViewDelegate <NSObject>
- (void)IPView:(UIView*)view finishEdit:(NSString *)text;
@end
@interface IPView : UIView <UITextFieldDelegate>
@property (nonatomic,assign)id<TPViewDelegate> delegate;
@end
IPView.m
- (void)pushMessage{
[_textView endEditing:YES];
if (str!= nil) {
[self.delegateIPView:self finishEdit:str];
}
self.hidden= YES;
}
ViewController.m
- (void)IPView:(UIView*)view finishEdit:(NSString *)text{
self.label.text= text;
}
2.闭包
BLView.h
typedefvoid(^textBlock)(NSString *);
@interfaceBLView : UIView <UITextFieldDelegate>
@property (nonatomic,copy)textBlockmyTextBlock;
@end
BLView.m
- (void)pushMessage{
[_textView endEditing:YES];
if (self.str!= nil) {
self.myTextBlock(self.str);
}
self.hidden= YES;
}
ViewController.m
_BLView.myTextBlock= ^(NSString *str){
self.label.text= str;
};
3.KVO
BLView.h
#import <UIKit/UIKit.h>
@interface BLView : UIView <UITextFieldDelegate>
@property (nonatomic,copy)NSString*str;
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
...
/**
* 被观察者
*
* @param NSObject 观察者
* @param KeyPath 被观察的属性路径
* @param options 返回的值的类型
{ NSKeyValueObservingOptionNew = 0x01, 变化之后的值
NSKeyValueObservingOptionOld = 0x02, 变化之前的值
}
* @param context 一般写nil
*
*/
[_BLView addObserver:selfforKeyPath:@"str"options:NSKeyValueObservingOptionNewcontext:nil];
}
// 观察者的方法
/**
* 当观察者 监视的属性发生变化时候调用的方法
*
* @param keyPath 观察的属性
* @param object 被观察的对象
* @param change 属性的值
* @param context nil
*/
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context{
self.label.text= change[@"new"];
@property (nonatomic,copy)textBlockmyTextBlock;
@end
BLView.m
- (void)pushMessage{
[_textView endEditing:YES];
if (self.str!= nil) {
self.myTextBlock(self.str);
}
self.hidden= YES;
}
ViewController.m
_BLView.myTextBlock= ^(NSString *str){
self.label.text= str;
};
3.KVO
BLView.h
#import <UIKit/UIKit.h>
@interface BLView : UIView <UITextFieldDelegate>
@property (nonatomic,copy)NSString*str;
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
...
/**
* 被观察者
*
* @param NSObject 观察者
* @param KeyPath 被观察的属性路径
* @param options 返回的值的类型
{ NSKeyValueObservingOptionNew = 0x01, 变化之后的值
NSKeyValueObservingOptionOld = 0x02, 变化之前的值
}
* @param context 一般写nil
*
*/
[_BLView addObserver:selfforKeyPath:@"str"options:NSKeyValueObservingOptionNewcontext:nil];
}
// 观察者的方法
/**
* 当观察者 监视的属性发生变化时候调用的方法
*
* @param keyPath 观察的属性
* @param object 被观察的对象
* @param change 属性的值
* @param context nil
*/
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context{
self.label.text= change[@"new"];
}
0 0
- iOS开发中 代理,block,KVO 的使用
- iOS中block和代理的使用
- iOS中代理、通知、block的使用
- ios开发——实用技术篇&Block/KVO/通知/代理
- iOS开发中KVO & KVC的使用
- iOS中KVO的使用
- iOS开发中Block的使用
- iOS开发中block的使用
- KVO、BLOCK、代理、通知
- iOS中 block 代理 通知的区别
- iOS 协议代理.block的基本使用
- iOS代理方法和block的使用
- iOS开发中KVO的内部实现
- [ios开发基础之]KVO的使用
- IOS开发之----KVO的使用
- IOS开发 - KVC和KVO的使用
- iOS中使用KVO
- iOS 代理,通知和KVO的基本使用
- ACM校赛 成功举办后总结
- NSFileManager的常用操作
- 开篇的话
- 安装gi的时候回退root.sh的执行
- spring mvc controller间跳转 重定向 传参
- iOS开发中 代理,block,KVO 的使用
- James 3.0 邮箱配置
- 确保对象的唯一性——单例模式 (二):负载均衡器的设计与实现
- Android之实现定位
- 一维指针作函数参数,与malloc需要手动释放
- JQuery初识
- Spring的一种拦截器SimpleUrlHandlerMapping
- Programming with ZooKeeper - A basic tutorial
- Sicily 1198. Substring