我們常常在內部某一個VC完成一個A task後,需要讓另一個VC能夠根據A task的結果或內容去做另一個B task,此時就需要在A task內加上通知,B task的VC加上一個observer,如此一來只要A task完成後發出notification,B task的觀察者馬上能夠知道A task已完成,然後去執行他該做的工作,可參考以下代碼
// Add an observer that will respond to loginComplete
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bTask:)
name:@"aTaskComplete" object:nil];
// Post a notification to loginComplete
[[NSNotificationCenter defaultCenter] postNotificationName:@"aTaskComplete" object:nil];
// the function specified in the same class where we defined the addObserver
- (void) bTask:(NSNotification *)note {
NSLog(@"Received Notification - Do something here!");
}
沒有留言:
張貼留言