Swift 3 - 在启动时加载多个 ViewController
Swift 3 - loading multiple ViewControllers at launch
我正在制作一个选项卡式应用程序。它附有一个 TabBarController 和 4 个 ViewController。
默认情况下,启动时仅加载 FirstViewController
。我想在开始时同时加载 FirstViewController
和 SecondViewController
,然后再通过选项卡菜单切换到第二个视图。
到目前为止我尝试的是创建自定义 MyTabBarController
class 并尝试使用
var sv = SecondViewController()
sv.loadView()
在 ViewDidLoad()
中,但它在加载期间导致了致命错误,因为(我的猜测)故事板中的 mapView 元素未加载。
同时加载两个使用故事板元素的viewController 的正确方法是什么?到目前为止,我所有的其他尝试都没有成功。
添加主视图控制器
var secondViewController:UIViewController!
并且在你的 viewDidLoad 中:
secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController
就是这样。当你想展示它时,使用:
self.present(secondViewController, animated: true, completion: nil)
我正在制作一个选项卡式应用程序。它附有一个 TabBarController 和 4 个 ViewController。
默认情况下,启动时仅加载 FirstViewController
。我想在开始时同时加载 FirstViewController
和 SecondViewController
,然后再通过选项卡菜单切换到第二个视图。
到目前为止我尝试的是创建自定义 MyTabBarController
class 并尝试使用
var sv = SecondViewController()
sv.loadView()
在 ViewDidLoad()
中,但它在加载期间导致了致命错误,因为(我的猜测)故事板中的 mapView 元素未加载。
同时加载两个使用故事板元素的viewController 的正确方法是什么?到目前为止,我所有的其他尝试都没有成功。
添加主视图控制器
var secondViewController:UIViewController!
并且在你的 viewDidLoad 中:
secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController
就是这样。当你想展示它时,使用:
self.present(secondViewController, animated: true, completion: nil)