iOS開發之觀察者

//觀察者事件  (!!註冊必刪除)

[[IndexViewInformation sharedInstance] addObserver:self forKeyPath:@"selectedMoreModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    

    [[IndexViewInformation sharedInstance] addObserver:self forKeyPath:@"selectedBuyModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];


//刪除註冊的觀察者事件

[[IndexViewInformation sharedInstance] removeObserver:self forKeyPath:@"selectedBuyModel"];


[[IndexViewInformation sharedInstance] removeObserver:self forKeyPath:@"selectedMoreModel"];


//觀察者觸發的事件,在觀察者觀察到變化的時候觸發

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    

    if ([keyPath isEqualToString:@"selectedMoreModel"]) {

        UIStoryboard *board=[UIStoryboard storyboardWithName:@"Main" bundle:nil];

        ProjectDetailViewController *pro =[board instantiateViewControllerWithIdentifier:@"ProjectDetail"];

        

        pro.shengyuTime = [IndexViewInformation sharedInstance].selectedMoreModel.thenDays;

        pro.projectID = [IndexViewInformation sharedInstance].selectedMoreModel.prodectId;

        pro.thenDays = pro.shengyuTime;

        [self.navigationController pushViewController:pro animated:YES];

        NSLog(@"觸發MoreModel觀察者~!");

        

    }

    

    if ([keyPath isEqualToString:@"selectedBuyModel"]) {

        

        NSLog(@"觸發BuyModel觀察者~!");

        

        [self requestBeforeBuy];

        

        

    }

    

}


再次強調,觀察者添加必有刪除,如果忘記刪除,會導致意想不到的麻煩。


本文有因爲問題請聯繫

QQ:563699115

Telephone:18341266547



發佈了28 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章