如何从另一个视图控制器实例化导航控制器?
我的目标是每当我点击第一个视图控制器上的button,然后它将导航到另一个控制器,这是一个导航控制器。
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 }
你正在实例化视图控制器,因此你不会得到导航栏。 要得到导航栏,请实例化导航控制器,因为第二个视图是唯一的孩子,你会得到默认的第二个视图。
@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) }
上面的代码应该给你导航栏。
在你的情况下,下面的代码应该是足够的:
self.tabBarController.selectedIndex = 1
由于UITabBarController
是您的rootViewController
,您可以通过self.tabBarController
访问它。 您不必像故事板中那样实例化UINavigationController
。
我可以从你的StoryBoard看到你有一个TabBarController。 如果你的configuration是这样的,FirstViewController在第二个选项卡的第一个选项卡和SecondViewController上,你可以改变TabBarController的selectedIndex属性:
@IBAction func buttonTapped(sender: UIButton) { tabBarController?.selectedIndex = 1 }
如果您想将数据传递给SecondViewController,您可以尝试以下解决scheme之一:
-
let controller = tabBarController.viewControllers[1] as SecondViewController! controller.data = "some data"
let controller = tabBarController.viewControllers[1] as SecondViewController! controller.data = "some data"
由于SecondViewController尚未准备就绪,因此无法工作。 - 创build一个单独的类来保存数据,然后在SecondViewController的viewDidLoad方法中检索数据
- 将数据保存在UserDefaults中,然后在SecondViewController中检索数据viewDidLoad方法(如果信息不必持久化,则为错误的解决scheme)
- 扩展UITabBarController并使用它,在tabBarController中创build一个自定义的variables,将数据放在该variables中,然后在SecondViewController中检索数据viewDidLoad方法
然后根据需要清除数据。
导航将不会从firstViewController工作,以导航我们需要的东西UINavigationController。
像这样尝试