Ios UINavigationController返回到多个步骤
是否可以在UINavigationController中弹出多个viewcontroller?假设我想后退两步。是的,你可以通过做类似的事情来实现Ios UINavigationController返回到多个步骤,ios,cocoa-touch,uinavigationcontroller,Ios,Cocoa Touch,Uinavigationcontroller,是否可以在UINavigationController中弹出多个viewcontroller?假设我想后退两步。是的,你可以通过做类似的事情来实现 //Your navigation controller UINavigationController *nav; //Get the view controller that is 2 step behind UIViewController *controller = [nav.viewControllers objectAtIndex:nav
//Your navigation controller
UINavigationController *nav;
//Get the view controller that is 2 step behind
UIViewController *controller = [nav.viewControllers objectAtIndex:nav.viewControllers.count - 2];
//Go to that controller
[nav popToViewController:controller animated:YES];
Swift
在swift中,Omar提出的相同解决方案为:
// Get the previous Controller.
let targetController: UIViewController = navigationController!.viewControllers[navigationController!.viewControllers.count - 2]
// And go to that Controller
navigationController?.popToViewController(targetController, animated: true)
在我的情况下,我需要备份2个控制器,因此,我必须在堆栈中进行第3次备份。我真正的解决办法是:
// obtaining origin controller
let controller: UIViewController = navigationController!.viewControllers[navigationController!.viewControllers.count - 2]
// If was the expected controller (An enroll action)
if controller is CreateChatViewController {
// I get the previous controller from it, in this case, the 3rd back in stack
let newControllerTarget = navigationController!.viewControllers[navigationController!.viewControllers.count - 3]
// And finally sends back to desired controller
navigationController?.popToViewController(newControllerTarget, animated: true)
}