第三個界面的值傳給第一個界面。
1. 在第一個界面建立一個通知中心, 通過通知中心,注冊一個監聽事件
2. 在第一個界面中,設置接收到通知的事件。
3. 在第一個界面中的dealloc中, 將通知中心remove掉
4. 在第三個界面中, 建立一個通知中心, 通過通知中心, 發送通知(發送通知的過程就是傳值的過程,將要傳輸的值作為object的值傳給第一個界面?
代碼片段:
第一界面:
//通知中心是個單例
NSNotificationCenter*notiCenter = [NSNotificationCenterdefaultCenter];
?// 注冊一個監聽事件。第三個參數的事件名, 系統用這個參數來區別不同事件。
[notiCenteraddObserver:selfselector:@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
{
//移除當前對象監聽的事件
[[NSNotificationCenterdefaultCenter]removeObserver:self];
[superdealloc];
}
第二界面:?
? ? ?// 創建一個通知中心
NSNotificationCenter*center = [NSNotificationCenterdefaultCenter];
? ? ?// 發送通知. 其中的Name填寫第一界面的Name, 系統知道是第一界面來相應通知, object就是要傳的值。 UserInfo是一個字典, 如果要用的話,提前定義一個字典, 可以通過這個來實現多個參數的傳值使用。
[centerpostNotificationName:@"cesuo"object:@"zhangheng"userInfo:dic];