gpt4 book ai didi

swift - 在第一次启动时显示另一个 View Controller ,而不是再次

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:04 28 4
gpt4 key购买 nike

我正在使用 swift 制作一个有两个 View Controller (主页、登录页面)的应用程序,我想在第一次启动时显示登录页面。

所以我使用了这段代码。

class ViewController: UIViewController {

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
if identifier == "LoginSegue" {

var segueShouldOccur : Bool

let isFirst:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isFirst")
if isFirst == false
{
segueShouldOccur = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isFirst")
}
else {
segueShouldOccur = false
}

if segueShouldOccur == true {
println("*** NOPE, segue wont occur")
return false
}
else {
println("*** YEP, segue will occur")
}
}

// by default, transition
return true
}

override func viewDidLoad() {
super.viewDidLoad()


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

我使用标识符为“LoginSegue”的segue 来显示登录页面。但是对于模拟器,它不显示登录页面。如何在第一次启动时显示登录页面?

最佳答案

您可以在 AppDelegate.swift 文件中的 didFinishLaunchingWithOptions 方法中编写所需的代码

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

//SET INITIAL CONTROLLER
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
if() //your condition if user is already logged in or not
{
// if already logged in then redirect to MainViewController

initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController // 'MainController' is the storyboard id of MainViewController
}
else
{
//If not logged in then show LoginViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginController") as! LoginViewController // 'LoginController' is the storyboard id of LoginViewController

}

self.window?.rootViewController = initialViewController

self.window?.makeKeyAndVisible()
return true
}

希望这能奏效!

关于swift - 在第一次启动时显示另一个 View Controller ,而不是再次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005900/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com