第一界面:
//通知中心是個單例
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
// 注冊一個監聽事件。第三個參數的事件名, 系統用這個參數來區別不同事件。
[notiCenter addObserver:self selector:@selector(receiveNotification:) name:@"cesuo" object:nil];
// @selector(receiveNotification:)方法, 即受到通知之后的事件
- (void)receiveNotification:(NSNotification *)noti
{
// NSNotification 有三個屬性,name, object, userInfo,其中最關鍵的object就是從第三個界面傳來的數據。name就是通知事件的名字, userInfo一般是事件的信息。
NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);
}
// 第一界面中dealloc中移除監聽的事件
- (void)dealloc
{
// 移除當前對象監聽的事件
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
第二界面:
// 創建一個通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 發送通知. 其中的Name填寫第一界面的Name, 系統知道是第一界面來相應通知, object就是要傳的值。 UserInfo是一個字典, 如果要用的話,提前定義一個字典, 可以通過這個來實現多個參數的傳值使用。
[center postNotificationName:@"cesuo" object:@"zhangheng" userInfo:dic];