需求背景:
從A界面跳轉到C,C界面點擊返回到B界面,B點擊返回回到A界面。
第一種情況:push + push
//在A控制器
[self.navigationController pushViewController:C animated:YES];
//獲取到導航控制器下的所有子控制器,設置好順序。
NSMutableArray*tempMarr =[NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//當前可以看到順序 tempMarr :[A,C]
[tempMarr insertObject:B atIndex:tempMarr.count- 1];
[self.navigationController setViewControllers:tempMarr animated:YES];
//添加之后順序tempMarr:[A,B,C],之后即可實現;
第二種情況:push + present
//更簡單 不需要獲取導航控制器,在A控制器中直接present到C控制器
[A presentViewController:C animated:YES completion:^{
//push去除動畫效果
[self.navigationController pushViewController:B animated:NO];
}];
第三種情況:present + push
[A presentViewController:[[UINavigationController alloc]initWithRootViewController:C] animated:YES completion:^{
NSMutableArray*tempMarr =[NSMutableArray arrayWithArray:C.navigationController.viewControllers];
[tempMarr insertObject:B atIndex:tempMarr.count -1 ];
[C.navigationController setViewControllers:tempMarr animated:YES];
}];
//這里值得注意的是此時這三個界面 是B,C在同一個導航控制器,A單獨在一個導航控制器,所以當C pop回到B的時候。B需要自定義一個返回到A的 按鈕
[self dismissViewControllerAnimated:YES completion:nil];
第四種情況 present + present
小知識點:A presentViewController 到B 后,
*A.presentedViewController就是B,
*B.presentingViewController就是A
其他的遇到這種情況的說明你得換個思維思考,換不了,說明是需求給的有問題,你可以去找老板談談,我也不會 ! 有會的大佬請指教