傳值方式
//單例 //.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的賦值 實現