3

我正在开发一个演练屏幕,它应该只在用户第一次打开应用程序时出现。到目前为止,我创建了演练页面和 PageViewController。看图片: 在此处输入图像描述

我在这里读了很多类似的问题,我明白我必须使用

  UserDefaults()

在 AppDelegate 中,但我不明白如何在代码中使用类和情节提要名称。基本上,当应用程序第一次打开时,PageViewController 应该出现在屏幕上,当用户单击开始按钮(位于 WalkThroughScreen 上)时,它将关闭教程页面并且应用程序将启动。

我试过这段代码:

if let isFirstStart = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool {
  if defaults.bool(forKey: "isFirstLaunch") {
    defaults.set(false, forKey: "isFirstLaunch")
    let mainStoryboard = UIStoryboard(name: "WalkThroughScreen", bundle: Bundle.main)
    let vc : WalkThroughScreen = mainStoryboard.instantiateViewController(withIdentifier: "PageViewController") as! WalkThroughScreen
    self.present(vc, animated: true, completion: nil)
     }

我很确定这完全是一团糟,因为我不太了解它,而且我没有使用过 TutorialPage,所以如果有人给我提示或示例如何正确操作,我将非常感激

4

2 回答 2

3

是的,你是对的,你必须使用userDefaults来实现这一点。你必须在appDelegate() Roel Koops 里面做 答案应该做,但你也可以这样尝试:

  let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
    if launchedBefore  {
        print("This is not first launch.")
    } else {
        print("This is first launch.")
        UserDefaults.standard.set(true, forKey: "launchedBefore")
        UserDefaults.standard.synchronize()
        let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc : WalkThroughScreen = mainStoryboard.instantiateViewController(withIdentifier: "WalkThroughScreen") as! WalkThroughScreen
        self.present(vc, animated: true, completion: nil)

    }

并确保声明:let userDefaults = UserDefaults.standard并在内部使用它didFinishLaunchingWithOptions

还有更多解决方案,所以我再给你一个:

    let userDefaults = UserDefaults.standard

    if !userDefaults.bool(forKey: "launchedBefore") {
        let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc : WelcomeViewController = mainStoryboard.instantiateViewController(withIdentifier: "WalkThroughScreen") as! WelcomeViewController
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController?.present(vc, animated: false, completion: nil)
        userDefaults.set(true, forKey: "launchedBefore")
        userDefaults.synchronize()

    }

您甚至可以在一行中声明所有情节提要:

self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "WalkThroughScreen")

但这假设您声明了 2 个变量:

var window: UIWindow?
var storyboard: UIStoryboard?

如果它不起作用,请告诉我,我会尽力提供帮助。

于 2016-11-01T22:53:44.453 回答
2

如果键“isFirstLaunch”不存在,则永远不会执行 if 块中的代码。

尝试这个:

    if let isFirstStart = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool {
        print("this is not the first launch")
    } else {
        print("this is the first launch")
        UserDefaults.standard.set(false, forKey: "isFirstLaunch")
        UserDefaults.standard.synchronize()

        let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc : WalkThroughScreen = mainStoryboard.instantiateViewController(withIdentifier: "WalkThroughScreen") as! WalkThroughScreen
        self.present(vc, animated: true, completion: nil)
    }
于 2016-11-01T22:29:52.740 回答