gpt4 book ai didi

ios - 如何从另一个 View Controller 实例化导航 Controller ?

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:04 26 4
gpt4 key购买 nike

我的目标是每当我点击第一个 View Controller 上的按钮时,它就会导航到另一个 Controller ,它是一个导航 Controller 。

firstViewController 和 secondViewController 没有任何联系。

图片

enter image description here

我用过这段代码

 @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"


}

最佳答案

您正在实例化 View Controller ,因此您不会获得导航栏。要获取导航栏,请实例化导航 Controller ,因为第二个 View 是唯一的 subview ,默认情况下您将获得第二个 View 。

@IBAction func buttonTapped(sender: UIButton) {

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Navigation Controller Id") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)

}

上面的代码应该给你导航栏。

关于ios - 如何从另一个 View Controller 实例化导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37992125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com