如何在应用首次运行时在我的iOS应用中打开页面视图控制器?

时间:2016-05-05 22:58:04

标签: ios swift

我正在为iOS创建一个应用程序,我想打开一个页面视图控制器(不是普通的视图控制器),但只是第一次用户打开应用程序。

问题是我无法在代码中打开新的页面视图控制器。用户将看到的第一个屏幕是登录屏幕,但在第一次访问时切换到页面视图控制器。

这是我到目前为止登录屏幕viewcontroller中的内容:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
    if launchedBefore  {
        //Not the first time, show login screen.
    }
    else {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
    }
    let secondViewController:InstructionViewController = InstructionViewController()

    self.presentViewController(secondViewController, animated: true, completion: nil)
}

我想要打开的页面视图控制器已在故事板上创建。

3 个答案:

答案 0 :(得分:4)

通过其他答案,我能够解决问题。

在appDeligate文件中,您需要将第一个函数替换为:

var window: UIWindow?
var storyboard:UIStoryboard?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    window =  UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.makeKeyAndVisible()

    let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")

    if launchedBefore  {
        //Not the first time, show login screen.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewControllerWithIdentifier("Login")

        if let window = self.window {
            window.rootViewController = rootController
        }

    }
    else {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewControllerWithIdentifier("Instruction")

        if let window = self.window {
            window.rootViewController = rootController
        }
    }

    return true
}

“登录”和“指令”是(页面)视图控制器的名称。

我不确定这是否是最强大的代码,但对我来说效果很好。

答案 1 :(得分:3)

App委托的 willFinishLaunchingWithOptions 方法将是最佳位置(AFAIK)。检查条件并相应地设置窗口的根视图控制器。

答案 2 :(得分:2)

以下是莱斯利的代码更新为Swift 3:

    window =  UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")

    if launchedBefore  {
        //Not the first time, show login screen.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewController(withIdentifier: "webView")

        if let window = self.window {
            window.rootViewController = rootController
        }

    }
    else {
        UserDefaults.standard.set(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewController(withIdentifier: "progressView")

        if let window = self.window {
            window.rootViewController = rootController
        }
    }

别忘了设置

var storyboard:UIStoryboard?

在didFinishLaunchingWithOptions之前,并设置类似于您自己的视图控制器标识符的标识符。