Swift 中的初始化程序
Initializer in Swift
假设我有 MainVC1 和 MainVC2,根据用户类型,我决定在应用程序启动时加载其中一个,没有太多事情要做选择哪个,但我想把它放在 AppDelegate 上是不对的,所以我创建了一个 VC 名称 PreMain 决定了这个,但问题是 AppStartUp 很丑导致 VC快速出现和消失 (thePreMain) 然后主电源将启动,有没有更好的方法来做到这一点?或者是否可以有一个没有实际视图的 ViewController?
困扰的方法是更改 AppDelegate 中的 rootViewController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var initialViewController: UIViewController!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if somecondition {
initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")//replace your FirstViewController identifier
} else {
initialViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")//replace your SecondViewController identifier
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
return true
}
but I guess its not right to put this on AppDelegate
选择AppDelegate来做那个逻辑没有错,毕竟在didFinishLaunching,你可以自由地做任何事情。
就像你说的,PreMain 会短暂出现。
这是可以帮助你的 linkProgrammatically set the initial view controller using Storyboards
避免任何"VC comes and disappear fast"
but I guess its not right to put this on AppDelegate
其实那是不对的!如果您尝试通过实例化预视图控制器来解决问题 - 正如您提到的那样 - 那就是 "not right" 情况。
如 application(_:didFinishLaunchingWithOptions:) 文档中所述:
Tells the delegate that the launch process is almost done and the app
is almost ready to run.
这似乎是确定所需初始视图控制器的良好起点。您可能想查看:set initial viewcontroller in appdelegate - swift 实现它的问答,您所要做的就是实现选择所需视图控制器的逻辑。
假设我有 MainVC1 和 MainVC2,根据用户类型,我决定在应用程序启动时加载其中一个,没有太多事情要做选择哪个,但我想把它放在 AppDelegate 上是不对的,所以我创建了一个 VC 名称 PreMain 决定了这个,但问题是 AppStartUp 很丑导致 VC快速出现和消失 (thePreMain) 然后主电源将启动,有没有更好的方法来做到这一点?或者是否可以有一个没有实际视图的 ViewController?
困扰的方法是更改 AppDelegate 中的 rootViewController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var initialViewController: UIViewController!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if somecondition {
initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")//replace your FirstViewController identifier
} else {
initialViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")//replace your SecondViewController identifier
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
return true
}
but I guess its not right to put this on AppDelegate
选择AppDelegate来做那个逻辑没有错,毕竟在didFinishLaunching,你可以自由地做任何事情。
就像你说的,PreMain 会短暂出现。
这是可以帮助你的 linkProgrammatically set the initial view controller using Storyboards
避免任何"VC comes and disappear fast"
but I guess its not right to put this on AppDelegate
其实那是不对的!如果您尝试通过实例化预视图控制器来解决问题 - 正如您提到的那样 - 那就是 "not right" 情况。
如 application(_:didFinishLaunchingWithOptions:) 文档中所述:
Tells the delegate that the launch process is almost done and the app is almost ready to run.
这似乎是确定所需初始视图控制器的良好起点。您可能想查看:set initial viewcontroller in appdelegate - swift 实现它的问答,您所要做的就是实现选择所需视图控制器的逻辑。