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

问题描述 投票:0回答:1

这个问题在这里已有答案:

在第一次下载我的应用程序时,我想推送一个不同的视图控制器(例如教程视图控制器),然后再次打开后,我想在启动时推动我的普通视图控制器。在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
}
ios swift viewcontroller
1个回答
1
投票

您可以尝试在UserDefaults中保存该状态

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

}
window!.makeKeyAndVisible()
© www.soinside.com 2019 - 2024. All rights reserved.