切换视图控制器取决于bool值

时间:2016-05-05 05:35:38

标签: ios swift

我在我创建的应用程序中有一个加载页面。我正在尝试设置教程。根据教程是否已经完成,加载页面将简单地切换视图控制器。

我希望应用程序在第一次打开时执行一次教程。我创建了两个segue。一个进入教程主屏幕,另一个进入屏幕,每次用户打开应用程序后。

我已将以下代码添加到加载页面,以确定是否完成了tutroial。我遇到的问题是我的加载页面什么也没做。它在加载时不执行代码。这是代码:

import Foundation
import UIKit

class FrontPageViewController: UIViewController  {

   var isTutorialComplete = false;

   override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view, typically from a nib. 
       if (isTutorialComplete == false) {

       //Pushes to tutorial home page
       self.performSegueWithIdentifier("ToTutorialHome", sender: nil)
       }
       else 
       {  
        //pushes to tech home page
        self.performSegueWithIdentifier("ToTechHome", sender: nil)
       }
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }
}

enter image description here

This is what the app is doing. I would like for it to not do this and move to a another view controller

1 个答案:

答案 0 :(得分:2)

将bool存储在 NSUserDefaults

第一次加载NSUserDefaults值时,如果他完成教程并单击“完成”,则必须将其设置为true并且已完成

  

这样做

let settingdefaults = NSUserDefaults.standardUserDefaults()

var isTutorialComplete = settingdefaults.boolForKey("onboarding")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        if (isTutorialComplete){


            //pushes to tech home page
            self.performSegueWithIdentifier("ToTechHome", sender: nil)
        }
        else {

             //Pushes to tutorial home page
            self.performSegueWithIdentifier("ToTutorialHome", sender: nil)


        }


    }
  

一旦他完成教程,就像这样

settingdefaults.setBool(true, forKey: "onboarding")
isTutorialComplete = settingdefaults.boolForKey("onboarding")