Swift 3-在启动时加载多个ViewController [英] Swift 3 - loading multiple ViewControllers at launch

查看:79
本文介绍了Swift 3-在启动时加载多个ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个选项卡式应用程序.它具有一个TabBarController和四个附加的ViewController.

I am working on making a tabbed app. It has a TabBarController and 4 ViewControllers attached to it.

默认情况下,在启动时仅加载FirstViewController.我想在通过选项卡菜单切换到第二个视图之前先加载FirstViewControllerSecondViewController.

By default, at launch only FirstViewController is loaded. I would like to load both FirstViewController and SecondViewController at start before switching to the second view via tab menu.

到目前为止,我尝试创建的是自定义MyTabBarController类并尝试使用

What i tried so far is that I created custom MyTabBarController class and tried to use

var sv = SecondViewController()
sv.loadView()

ViewDidLoad()中,但是在加载过程中导致了致命错误,因为(我猜)故事板中的mapView元素未加载.

in ViewDidLoad(), but it caused a fatal error during loading, because (my guess) mapView element from storyboard was not loaded.

同时加载使用情节提要元素的两个viewController的正确方法是什么?到目前为止,我所有其他尝试都没有成功.

What is the correct way to simultaneously load the two viewControllers which use storyboard elements? All my other tries have not been successful so far.

推荐答案

在主视图控制器中添加

var secondViewController:UIViewController!

在您的viewDidLoad中:

And in your viewDidLoad:

secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController

就是这样.当您要显示它时,请使用:

That's it. When you want to present it, use:

self.present(secondViewController, animated: true, completion: nil)

这篇关于Swift 3-在启动时加载多个ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆