星空网 > 软件开发 > 操作系统

iOS 之单例,代理,通知,KVO,Block全能解析

//单例//.h+ (Instannce *)shareInstance;//.mstatic 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;//.mif ([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的赋值 实现

 




原标题:iOS 之单例,代理,通知,KVO,Block全能解析

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

商标TM必须标注吗?:https://www.kjdsnews.com/a/1388979.html
商标R和T的明显区别及其应用:https://www.kjdsnews.com/a/1388980.html
商标R和T的区别及其对商标保护的重要性:https://www.kjdsnews.com/a/1388981.html
商标R号的申请规则及使用限制:https://www.kjdsnews.com/a/1388982.html
商标CS详细了解商标注册流程:https://www.kjdsnews.com/a/1388983.html
商城注册商标抢占市场先机的重要步骤:https://www.kjdsnews.com/a/1388984.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流