需求場景,例如在購物車模塊,我們一直往下走會走到支付成功頁面,這時可能就會需要我們點擊返回按鈕或者返回首頁按鈕,跳轉到首頁模塊,解決方案如下:
PaySuccess.m 響應返回的事件中
//創建一個消息對象
NSNotification * notice = [NSNotification notificationWithName:@"BackHome" object:nil userInfo:nil];
//發送消息
[[NSNotificationCenter defaultCenter]postNotification:notice];
[self.navigationController popToRootViewControllerAnimated:YES];
CarViewcontroller.m 中的處理
//獲取通知中心單例對象
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
//添加當前類對象為一個觀察者,name和object設置為nil,表示接收一切通知
[center addObserver:self selector:@selector(backHome) name:@"BackHome" object:nil];
-(void)backHome {
//注意index實際應用時位置,不要越界或者寫一個不存在的
self.tabBarController.selectedIndex = 0;
}