我有一个带有 2 个选项卡的 tabBarController
。每个选项卡内都有一个 navigationController
。每个 navigationController
都有 3 个子 viewControllers
tabBarController
/ \
tabOne tabTwo
/ \
NumNavController ColorNavController
| |
ViewOne ViewBlack
| |
ViewTwo ViewRed
| |
ViewThree ViewPurple//(beginning)
|
myButton
//myButton pushes on ViewOne>ViewTwo>ViewThree(end)
我在 ViewPurple 中有一个按钮,按下时我想按下 ViewOne、ViewTwo,然后是 ViewThree。
(beginning)ViewPurple > ViewOne > ViewTwo > ViewThree(end)
因此,我会从 ViewThree 推回到 ViewPurple-这就是我想要实现的目标
(beginning)ViewPurple < ViewOne < ViewTwo < ViewThree(end)
在 ViewPurple 内部,我将 ColorNavController 的子 Controller 重置为 NumNavController 的子 Controller 。
let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
self.navigationController?.setViewControllers(numVCs, animated: true)
//Apple docs says to set them backwards
//The view controllers to place in the stack. The front-to-back order of the controllers in this array represents the new bottom-to-top order of the controllers in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack
我遇到的问题是,一旦我按下 ViewOne 并按下后退按钮而不是转到 ViewPurple,我最终会进入 ViewTwo。它陷入了一个奇怪的循环,它似乎不知道开始的 viewController 在哪里。
//I can't push back to ViewPurple
ViewOne < ViewTwo < ViewOne < ViewTwo < ViewThree(end)
问题出在哪里,如何返回 ViewPurple?
ViewPurple:(第一种方式 - 没有崩溃但奇怪的循环)
class ViewPurple: UIViewController {
@IBAction func myButton(sender: UIButton){
self.presentNewVCs()
}
func presentNewVCs(){
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController
let viewTwoVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoController
let viewThreeVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewThree") as! ViewThreeController
let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
self.navigationController?.setViewControllers(numVCs, animated: true)
}
}
我还尝试了第二种方式来呈现 NumNavController,但我一直收到异常 'Application tried to present modally an active controller
。我对 ViewPurple 进行了弱引用并尝试取消初始化它,但它仍然崩溃了
ViewPurple:(第二种方式 - 崩溃)
class ViewPurple: UIViewController {
weak var parent:ViewPurple!
@IBAction func myButton(sender: UIButton){
self.presentNewVCs()
}
func presentNewVCs(){
let numNavController = self.tabBarController!.viewControllers![0] as! NumNavController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController
let viewTwoVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoController
let viewThreeVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewThree") as! ViewThreeController
let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
numNavController.setViewControllers(numVCs, animated: true)
self.presentViewController(numNavController, animated: true, completion: nil)
}
deinit{
self.parent = nil
}
}
最佳答案
能否请您更新您的buttonAction:
@IBAction func myButton(sender: UIButton){
let viewOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController
self.navigationController.pushViewController(viewOneVC, animate:true)
}
关于ios - 如何将一组新的 View Controller 从一个选项卡推送到另一个选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41423578/