ios - 根据 bool 值切换 View Controller

标签 ios swift

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

我想让应用程序在第一次打开时执行一次教程。我创建了两个segue。一个转到教程主屏幕,另一个将在用户每次打开应用程序时转到该屏幕。

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

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

最佳答案

将 bool 值存储在 NSUserDefaults 中。

当你第一次加载 NSUserDefaults 值时,当他完成教程并单击 done 时,你必须将其设置为 true 并完成

Do it like this

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)


        }


    }

Once he finish the tutorial do like this

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

关于ios - 根据 bool 值切换 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043153/

相关文章:

ios - 移动 Safari (iOS) : <input type ="file"> does no longer convert + downscale video

ios - block 快速移动时的意外碰撞行为 - Sprite 通过

ios - ContentInsets 不适用于 UITableView

ios - ‘无法在属性初始值设定项中使用实例成员 ‘allEvents’;属性初始值设定项在 ‘self’ 可用之前运行’错误

ios - AVAudioEngine 麦克风启动时崩溃

ios - 表格 View 中的自定义单元格不显示内容

ios - 当文本下降到 Swift 中的键盘级别时,如何在我的 TextView 中实现自动滚动?

ios - 如何去除 UITableView 顶部和底部的阴影?

ios - 如何解决此错误 "Cannot invoke getObejectAtPath with argument list type of string"

ios - 尝试删除对象类型的子对象时 Realm 崩溃