如何在首次启动后更改初始视图控制器?

时间:2016-11-23 23:43:11

标签: ios swift uiviewcontroller

我有一个包含教程的视图控制器,我只想在第一次打开应用程序时将其显示为初始视图。之后,我想呈现我的主视图控制器。我在这里看了一些关于这个的问题,但说实话,我什么都听不懂。有人能帮帮我吗?

2 个答案:

答案 0 :(得分:4)

在您的应用程序启动后,它将调用应用程序委托中的didFinishLaunchingWithOptions方法:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // here you have the chance to change your rootview controller
    if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
        UserDefaults.standard.set(true, forKey: "notFirstInApp")
        window?.rootViewController = your tutorial view controller
    }else{
        window?.rootViewController = your main viewcontroller
    }

    return true
}

答案 1 :(得分:1)

如果您想要视图或浏览视图,请使用

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "youSegueIdentifire"
    {
        if let destinationVC = segue.destinationViewController as? OtherViewController {
            destinationVC.numberToDisplay = counter
        }
    }
}

并调用此segue

self.performSegueWithIdentifier("youSegueIdentifire", sender: self)

如果你想在谈论初始viewcontroller

时更改根视图
var exampleViewController: ExampleViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ExampleController") as! ExampleViewController

    self.window?.rootViewController = exampleViewController