iOS 之单例,代理,通知,KVO,Block全能解析
2024-08-27 21:10:24
//单例
//.h
+ (Instannce *)shareInstance;
//.m
static Instannce *instance = nil;
@implementation Instannce
//定义一个创建单例对象的方法
+ (Instannce *)shareInstance {
if (instance == nil) {
instance = [[Instannce alloc] init];
}
return instance;
}
//使用alloc的时候调用的方法instancetype
+ (id)allocWithZone:(struct _NSZone *)zone {
if (instance == nil) {
instance = [super allocWithZone:zone];
}
return instance;
}
- (id)copy {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
//返回无符号最大值
return UINT_MAX;
}
- (oneway void)release {
//什么也不做
} //代理
//.h
@protocol GetMessageProtocol <NSObject >
- (void)getNum:(NSString *)num withPassWord:(NSString *)pass;
@end
@property (nonatomic,assign) id<GetMessageProtocol> delegate;
//.m
if ([self.delegate respondsToSelector:@selector(getNum:withPassWord:)]) {
[self.delegate getNum:num.text withPassWord:passWord.text];
}
#pragma mark - GetMessageProtocol
- (void)getNum:(NSString *)num withPassWord:(NSString *)pass { }
registerCtrl.delegate = self; //通知 注意postNotificationName 必须一致
[[NSNotificationCenter defaultCenter] postNotificationName:NotificationName object:self userInfo:dic]; //dic存放在userinfo中 dic中存放要传过去的值是个字典
//接受通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:NotificationName object:nil]; //KVO监听
/*KVO观察者方法
keyPath: 监听的属性名
object: 被观察的对象
change: 属性值
context: 上下设备文
*/
[registerCtrl addObserver:self forKeyPath:@"属性名称1" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
[registerCtrl addObserver:self forKeyPath:@"属性名称2" options:NSKeyValueObservingOptionNew context:nil];
//触发的事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//object的值是registerCtrl
if ([keyPath isEqualToString:@"属性名称1"]) { }else if ([keyPath isEqualToString:@"属性名称2"]) { }
}
//.h
@property (nonatomic, copy) NSString *属性名称1;
@property (nonatomic, copy) NSString *属性名称2;
//.m 必须通过setter方法改变值或者KVC //KVO方式
//触发的事件
[indexCollectionView addObserver:self forKeyPath:@"属性名称" options:NSKeyValueObservingOptionNew context:nil];
[posterCollectionView addObserver:self forKeyPath:@"pathIndex" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//得到改变后的新值
NSInteger index = [[change objectForKey:@"new"] integerValue]; }
} //Block block的返回值 block的名称 block的参数
typedef void(^SucccessBlock)(NSString *); //Block的定义
@property(nonatomic,copy)SucccessBlock loginBlock; //block的声明 要用copy防止block的循环引用
_freindBlcok(friends); block的调用
[[MyXMPPManager shareManager] getFreind:^(NSArray *freinds) {} //block的赋值 实现
最新文章
- python学习之day4,函数
- SHA1算法
- 报表session与应用session常识普及
- 【Android】Android ListViewAnimations分析
- code forces Jeff and Periods
- perl 学习笔记
- 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)
- Java多线程:线程同步与关键字synchronized
- UE4 多人FPS VR游戏制作笔记
- JVM GC-----垃圾回收算法
- 委托(4).net 3.5中的委托
- matlab2014a 转化c语言
- python练习--利用while循环和if语句,完成猜骰子的数字大小
- 实验十&#160;ZStack&#160;网状网络实验
- LeetCode(110):平衡二叉树
- VS2013 C#中调用DLL
- day39 mysql数据库基本操作
- hadoop运行一段时间后无法stop-all的问题
- [转]asp+oracle分页
- CSS渐变之CSS3 gradient在Firefox3.6下的使用
热门文章
- Educational Codeforces Round 4 B. HDD is Outdated Technology 暴力
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论
- Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题
- mysql 数据库性能追踪与分析
- POJ1651:Multiplication Puzzle(区间DP)
- Golang学习 - reflect 包
- jQuery刷新div内容,并对刷新后元素绑定事件。$(document).on()
- C#属性(Attribute)用法实例解析
- SQL Server 的事务和锁(一)
- [WinForm] 使用反射将业务对象绑定到窗体或控件容器