如何仅在首次启动iOS应用程序(Swift)时呈现不同的视图控制器-以编程方式

时间:2019-02-27 15:51:41

标签: ios swift viewcontroller

首先下载我的应用程序时,我想推送一个不同的视图控制器(例如教程视图控制器),然后再次打开后,我想在启动时推送我的普通视图控制器。快速地,我推送视图控制器的方式如下:

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
}

1 个答案:

答案 0 :(得分:1)

您可以尝试将该状态保存到UserDefaults

if !UserDefaults.standard.bool(forKey: "LaunchedBefore") {
    window!.rootViewController = LaunchViewController()
    UserDefaults.standard.set(true, forKey: "LaunchedBefore")
} else {
    window!.rootViewController = ViewController()

}
window!.makeKeyAndVisible()