在第一次启动时显示另一个视图控制器,而不是再次 [英] Show another view controller at the first launch and not again
本文介绍了在第一次启动时显示另一个视图控制器,而不是再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用swift创建一个具有两个视图控制器的应用程序(主页面,登录页面),我想在第一次启动时显示登录页面。
I am making an application with swift that has two view controllers(main page, login page) and I want to show login page at the very first launch.
所以我使用了这段代码。
So I used this code.
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来显示登录页面。
但是使用模拟器它不会显示登录页面。
如何从首次启动时显示登录页面?
I used the segue with identifier "LoginSegue" to show login page. But with simulator it doesn't show login page. How can I show login page from the first launch?
推荐答案
您可以在<$ c中编写所需的代码$ c> 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
}
希望这会有效!
这篇关于在第一次启动时显示另一个视图控制器,而不是再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文