【发布时间】:2017-01-02 08:38:25
【问题描述】:
我有一个带有 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 重置为 NumNavController 的子控制器。
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
}
}
【问题讨论】:
-
为什么要同时插入三个 Viewcontroller。你很好地解释了你的问题,但对我来说它不是 100% 可以理解的。 :(
-
@Dheeraj D 你是什么意思?我想从 PurpleVC 转到一、二和三。我需要遍历所有三个。你是说我不必那样做?
-
创建从 Purple View 到 ViewOne 的 push segue 或从 Purple View 创建 Modal segue 到
NumNavController。 -
你可以做到这一点....我会告诉你怎么做。我有一些问题请回答....因为我对您的问题有些困惑。
-
@Mr.Bista 我尝试了一个程序化的推送序列,但我意识到 tabBar 和其他 numNavController 妨碍了。我会再试一次。
标签: ios uinavigationcontroller swift2 uitabbarcontroller viewcontroller