我有两个不同的入口点:
let entryPoint: UITabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
和
let entryPoint: WelcomeController = mainStoryboard.instantiateViewControllerWithIdentifier("WelcomeController") as! WelcomeController
如何将 instantiateViewControllerWithIdentifier
设置为此全局 entryPoint
var?这对我来说很困难,因为它们有两种不同的 ViewController 类型:UITabBarController
和 ViewController
。
所以,我想要像
var entryPoint?
以后
entryPoint: UITabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
或
entryPoint: WelcomeController = mainStoryboard.instantiateViewControllerWithIdentifier("WelcomeController") as! WelcomeController
最佳答案
解决方案将取决于您以后打算如何使用 entryPoint。
如果您没有任何特定于类的用途或不介意一些类型转换,您始终可以将入口点声明为 UIViewController:
var entryPoint:UIViewController?
swift 将接受这两个分配,因为所有类都是 UIViewController 的子类。
如果以后要使用特定的属性,则需要检查变量的实际类
例如:
if let tabBarEntry = entryPoint as? UITabBarController
{
tabBarEntry.selectedIndex = 1
}
关于ios - 如何将 instantiateViewControllerWithIdentifier 设置为全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783769/