我的目标是每当我单击第一个视图控制器上的按钮时,它将导航到另一个控制器,即导航控制器。
firstViewController和secondViewController没有任何连接。
图片
我用了这段代码
@IBAction func buttonTapped(sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("secondViewCtrl") as! SecondViewController
self.presentViewController(vc, animated: true, completion: nil)
}
我之所以如此低落以使我可以像这样传递数据的原因
vc.name = "Myname"
此代码的问题在于,它不显示导航栏和选项卡栏。我该怎么做才能显示两者?
更新的问题
@IBAction func buttonTapped(sender: UIButton) {
guard let tabBarController = tabBarController else { return }
tabBarController.selectedIndex = 1
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("trackYourGenie") as! TrackYourGenieViewController
let navController = tabBarController.viewControllers![1]
let secondViewController = navController.topViewController
vc.name = "Myname"
}
一种安全的方法:
guard let tabBarController = tabBarController else { return }
tabBarController.selectedIndex = 1
如果您需要访问tabBarController来传递数据,则可以简单地执行以下操作:
let navController = tabBarController.viewControllers[1]! as! UINavigationController
let secondViewController = navController.topViewController
您的方法可能是:
@IBAction func buttonTapped(sender: UIButton) {
guard let tabBarController = tabBarController else { return }
let navController = tabBarController.viewControllers[1]! as! UINavigationController
let secondViewController = navController.topViewController as! SecondViewController
secondViewController.name = "my name"
tabBarController.selectedIndex = 1
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句