1. 程式人生 > >IOS NSNotificationCenter訊息通訊機制介紹

IOS NSNotificationCenter訊息通訊機制介紹

訊息傳送機構:舉例說明


在有需要的類中,傳送訊息
//傳送訊息出去,這裡的物件是一個數組:saveImageArray
[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:saveImageArray];

所有親朋好友給我給包(傳送訊息),,,



//註冊一個observer來響應訊息的傳送
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(PostImage:)//接收訊息方法
                                                 name:@"postData"//訊息識別名稱
                                               object:nil];
                                              
舉個例子,過年了,準備一個大的錢包(註冊一個OBserver),嘿嘿,,,,                                              
           
               
                                  
                       
//實現方法           
-(void)PostImage:(NSArray *)array
{
    //接收傳送過來的訊息
}       

我把紅包都收起來,(接收訊息)           
               
               
                   
                                              
//移除observer                                          
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"postData" object:nil];
紅包都收完了,哈哈,親朋好友回家咯。。



簡單說明,哈哈,不需要理解太複雜。。。。

例說明:KVC用來傳送訊息,是很不錯的。


//kvo註冊以parserDatas為例說明:parserDatas是一個解析XML的類
[parserDatas addObserver:self forKeyPath:@"isFinished"    options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:parserDatas];




//接收變更通知
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{
   
    if ([keyPath isEqual:@"isFinished"]) {
        BOOL isFinished=[[change objectForKey:NSKeyValueChangeNewKey] intValue];
        if (isFinished) {//如果伺服器資料接收完畢
           
            NSMutableArray *array = [[NSMutableArray alloc] init];
            [array addObjectsFromArray:parserDatas.parsedDataArray];
            //儲存資料
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            NSString *path = [DOCUMENT_PATH stringByAppendingPathComponent:@"AllXmlDataArray.bin"];
            [NSKeyedArchiver archiveRootObject:array toFile:path];
            [array release];
            //取消kvo註冊
            [object removeObserver:self
                     forKeyPath:@"isFinished"];
        }     
    }else{
        // be sure to call the super implementation
        // if the superclass implements it
        [super observeValueForKeyPath:keyPath
                             ofObject:object
                               change:change
                              context:context];
    }
}




在parserDatas類中定義:

+ (BOOL):(NSString*)key
{
    //當這兩個值改變時,使用自動通知已註冊過的觀察者,觀察者需要實現observeValueForKeyPath:ofObject:change:context:方法
    if ([key isEqualToString:@"isFinished"])
    {
        return YES;
    }
    return [super automaticallyNotifiesObserversForKey:key];
}



傳送變更通知:

在需要跟蹤訊息記錄的函式中加入:
[self setValue:[NSNumber numberWithBool:YES] forKey:@"isFinished"];