我有一个视图控制器作为我的初始视图控制器。 当它有一个按钮(GO按钮),当用户点击时,它应该转到另一个视图控制器(我们称之为目的地视图控制器,标签为'这是我想去的地方')。同时我想通过Tabbar控制器传递它。原因是我希望在我的导航堆栈中有tabbar,当用户按下目标视图控制器时,它必须转到tabbar控制器。图片展示了我想要的东西。如何在导航堆栈中跳过tabbar我该怎么办?
答案 0 :(得分:2)
您可以在GO按钮的IBAction
内轻松完成:
@IBAction func goTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc1 = storyboard.instantiateViewController(withIdentifier: "myTabBarViewController")
let vc2 = storyboard.instantiateViewController(withIdentifier: "myGoalViewController")
let controllers = [vc1, vc2]
self.navigationController!.setViewControllers(self.navigationController!.viewControllers + controllers, animated: true)
}
祝你好运!
答案 1 :(得分:1)
可以手动转到DestinationViewController:
if let destinationViewController = self.storyboard?.instantiateViewController(withIdentifier: "Storyboard ID of DestinationViewController") {
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
(或者,你可以在Storyboard中直接从FirstViewController到DestinationViewController)
在您的DestinationViewController中,在视图出现后手动将TabbarController插入导航序列,然后您就可以返回到TabbarController:
class DestinationViewController: UIViewController {
//......
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if self.isBeingPresented || self.isMovingToParentViewController {
var viewControllers = self.navigationController?.viewControllers
if let index = viewControllers?.endIndex.advanced(by: -1),
let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "Storyboard ID of TabBarController") {
viewControllers?.insert(tabBarController, at: index)
self.navigationController?.viewControllers = viewControllers!
}
}
}
//......
}