你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> iOS 之單例,代理,通知,KVO,Block全能解析

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

編輯:IOS技巧綜合
[摘要]本文是對iOS 之單例,代理,通知,KVO,Block全能解析的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
//單例
//.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的賦值 實現
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved