通知中心

    //通知中心沒有依賴性, 只要是兩個頁面存在

    //通知中心的使用

    //1. 獲取通知中心, 註冊一個觀察者和事件

 //這是一個單例類

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    

    //再通知中心中, 添加一個觀察者和觀察事件

    //參數1: 負責相應事件的觀察對象

    //參數2: 一旦收到消息, 觀察者要執行的方法

    //參數3: 觀察者要監聽的事件

    //參數4: 可以限定消息發出者

    [center addObserver:self selector:@selector(receiveNotification:) name:@"上廁所" object:nil];

//收到通知中心的消息時, 觀察者(self)要調用的方法

- (void)receiveNotification:(NSNotification *)noti

{

    NSLog(@"%@", noti.object);

    self.view.backgroundColor = [UIColor blackColor];

}












- (void)buttonAction:(UIButton *)button

{

    //通知中心的使用

    //發送一個消息

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    //參數1: 發送消息的事件名

    //參數2: 可以使用這個參數, 傳遞一個對象給觀察者

    //參數3: 一些消息的參數信息(系統用的比較多)

    [center postNotificationName:@"上廁所" object:@"香皂" userInfo:nil];

    

    [self.navigationController popToRootViewControllerAnimated:YES];

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章