作用同方案一(http://www.jianshu.com/p/f8befb7304db)
但是代码和原理更加简单
情景
Nav1下的A --push—>B B —Push—>C,但是C是再Nav2下的
原理:
1.在(B —Push—>C)过程中,在B中做如下操作
// 给Nav2的rootViewController传输信息
NIMSession *session = [NIMSession session:chatToken type:NIMSessionTypeP2P];
messageListViewController.shouldJumpToChat = YES;
ChatModel *chatModel = [[ChatModel alloc] initWithSession:session
chatName:chatName
chatToken:chatToken
chatAvatar:chatAvatar
currentCatId:chatCurrentCatId];
messageListViewController.chatModel = chatModel;
// 判断当前navigationController是不是nav2
// NO tabbarController选到Nav2的item上
// 当前navigationController要popToRoot
if ([self.navigationController isEqual:nav2])
{
[self.navigationController popToRootViewControllerAnimated:NO];
}
else
{
// 这里要注意下:要先切换到Nav2上.再把老的navigationController来popToRoot
[self.tabBarController setSelectedIndex:MessageIndex];
[self.navigationController popToRootViewControllerAnimated:NO];
}
2.在Nav2的rootViewController的viewWillAppear或者viewDidAppear中做如下操作
if (self.chatModel && self.shouldJumpToChat)
{
// 这个dispatch在处理已经在nav2下进行pop的时候会有用,不加的话会导致pop到nav2的rootViewController后在viewWillAppear或者viewDidAppear确实走了跳转的代码,但是不执行跳转
// 原因是pop和push两个操作执行时间太紧凑,导致push失败,这个时候用 在主线程中异步调用,把这些操作往后排(异步的作用),来延时push,相当于空闲时调用
dispatch_async(dispatch_get_main_queue(), ^{
// 用拿到的信息创建要跳转的控制器
NIMSession *session = self.chatModel.session;
MessageViewController *sessionVC = [[MessageViewController alloc] initWithSession:session];
sessionVC.chatName = self.chatModel.chatName;
sessionVC.chatToken = self.chatModel.chatToken;
sessionVC.chatAvatar = self.chatModel.chatAvatar;
sessionVC.currentCatId = self.chatModel.currentCatId;
// 跳转
[sessionVC setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:sessionVC animated:YES];
// 信息置为空
self.chatModel = nil;
self.shouldJumpToChat = NO;
});
}
3.其他注意点
pop操作的动画最好置为NO,不然在已经是Nav2的情况下会蛋疼
[self.navigationController popToRootViewControllerAnimated:NO];
push操作的动画用YES,情理之中
[self.navigationController pushViewController:sessionVC animated:YES];
虽然tabbarController从Nav1到Nav2的代码再pop之前,但是调用popToRootViewControllerAnimated:NO的self.navigationController还是Nav1而不是Nav2
而且必须保持下面顺序,不能调换
[self.tabBarController setSelectedIndex:MessageIndex];
[self.navigationController popToRootViewControllerAnimated:NO];