[英]How to initialise View Controller in a Project with Two Storyboards iOS Swift
从您的plist中删除main.storyboard加载,并更改您的didfinishwithlaunchingoptions函数:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "XXX", bundle: nil) let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC") self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() return true
}
在Plist上,设置要使用的起始Storyboard。 默认情况下,它是main。 **注意:XXX用于表示您要使用的任何新文件的名称。
一种使Storyboard变得紧张的干净方法。 在本教程的后面,您将找到一种获取ViewController实例的干净方法。
enum AppStoryboard: String {
case main = "Main"
case preLogin = "PreLogin"
case timeline = "Timeline"
var instance : UIStoryboard {
return UIStoryboard(name: rawValue, bundle: Bundle.main)
}
}
// USAGE :
let storyboard = AppStoryboard.Main.instance
关于您的问题:在项目Plist上,设置第一个Storyboard。 默认情况下是主要的。
您应该知道两个概念:“应用程序的默认界面”和“情节提要的默认场景(视图控制器)”。
在Info.plist上设置了“应用程序的默认界面”,请参见下图
应用程序的默认界面
isInitialViewController
设置“情节isInitialViewController
默认场景(视图控制器)”
因此,如果要在Main.storyboard中启动MainViewController
,则应将默认接口设置为Main.storyboard
如果要在Custom.storyboard中初始化CustomViewController
,则可以在CustomViewController
中将isInitialViewController
设置为true。
然后,您可以通过UIStoryboard(name: "Custom", bundle: Bundle.main).instantiateInitialViewController()
获取CustomViewController
实例。
而且,如果要初始化不是UIStoryboard(name: "Custom", bundle: Bundle.main).instantiateViewController(withIdentifier: "yourViewController")
的ViewController,则可以通过UIStoryboard(name: "Custom", bundle: Bundle.main).instantiateViewController(withIdentifier: "yourViewController")
获取实例。 在此之前,您应该在情节Storyboard ID
中设置Storyboard ID
。 故事板文件中的故事板ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.