简体   繁体   English

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

[英]How to open a page view controller in my iOS app only the first time the app runs?

I am creating an app for iOS and I want to open a page view controller (not a normal view controller) but only the first time that a user opens the application. 我正在为iOS创建一个应用程序,我想打开一个页面视图控制器(不是普通的视图控制器),但只是第一次用户打开应用程序。

The problem is that I can't open a new page view controller within the code. 问题是我无法在代码中打开新的页面视图控制器。 The first screen that the users will see is the login screen, but on first visit switch to the page view controller. 用户将看到的第一个屏幕是登录屏幕,但在第一次访问时切换到页面视图控制器。

This is what I have so far in the login screen viewcontroller: 这是我到目前为止在登录屏幕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)
}

The page view controller that I want to open is already created on the storyboard. 我想要打开的页面视图控制器已在故事板上创建。

With the other answers I was able to solve the problem. 通过其他答案,我能够解决问题。

In the appDeligate file you need to replace the first function with this: 在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
}

"Login" and "Instruction" are the names of the (page) view controllers. “登录”和“指令”是(页面)视图控制器的名称。

I am not sure if this is most robust code but it works fine for me. 我不确定这是否是最强大的代码,但它对我来说很好。

App delegate's willFinishLaunchingWithOptions method would be the best place (AFAIK). App委托的willFinishLaunchingWithOptions方法将是最好的地方(AFAIK)。 check the condition and set window's root view controller accordingly. 检查条件并相应地设置窗口的根视图控制器。

Here is Lesley's code updated to Swift 3: 这是Lesley的代码更新为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
        }
    }

Don't forget to set the 别忘了设置

 var storyboard:UIStoryboard? 

Before didFinishLaunchingWithOptions, and to set the identifiers similar to your own view controllers identifiers. 在didFinishLaunchingWithOptions之前,并设置类似于您自己的视图控制器标识符的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS应用仅在首次使用该应用时发送提醒视图 - iOS app sending an alert view only on the first time using the app 创建仅在首次打开应用程序时出现的View Controller - Creating a View Controller that only appears on first app open 如何使视图控制器仅在用户第一次快速打开应用程序时出现 - How to make a view controller only appear the first time a user opens the app in swift 如何制作仅在应用程序首次打开时显示的视图控制器? - How to make an view controller, which displays only when app opens first time? iOS和Xcode:仅在首次使用应用程序时如何为特殊视图创建模式选择 - iOS and xcode: how to create a modal segue to a special view only the first time the app is used 如何确定用户第一次运行应用程序? - How to determine that user runs the app for the first time? iOS:如何更改应用程序中仅一个视图控制器的方向? - iOS: How to change Orientation of only one view controller in app? 如何检测我的应用程序的首次启动并运行指定的视图控制器? - How to detect first launch of my app and run a specified view controller? 如何仅在我第一次访问 iOS 应用程序时调用 class - How to call a class only the first time i access an iOS app APP 只能在 ios 上第一次运行 - APP only works the first time on ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM