这个问题在这里已有答案:
在第一次下载我的应用程序时,我想推送一个不同的视图控制器(例如教程视图控制器),然后再次打开后,我想在启动时推动我的普通视图控制器。在swift中,我推动视图控制器的方式如下:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
如此直观,可能是这样的:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
if firstRun == true {
window!.rootViewController = LaunchViewController()
window!.makeKeyAndVisible()
} else {
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
}
return true
}
您可以尝试在UserDefaults
中保存该状态
if !UserDefaults.standard.bool(forKey: "LaunchedBefore") {
window!.rootViewController = LaunchViewController()
UserDefaults.standard.set(true, forKey: "LaunchedBefore")
} else {
window!.rootViewController = ViewController()
}
window!.makeKeyAndVisible()