如何在成功登录后更改初始视图控制器?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在成功登录后更改初始视图控制器?相关的知识,希望对你有一定的参考价值。
我正在开发我的第一个应用程序,我无法理解如何在用户成功登录后使用NSUserDefaults
更改我的初始视图控制器。(一旦用户成功登录,用户将不再需要在启动应用程序后登录)我已经阅读了其他帖子和google教程,但老实说我无法理解它们。
我主要是格式问题。
编辑:
这是我尝试添加的新代码:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
if PFUser.currentUser() != nil {
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Timeline") as! UIViewController
}else {
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
但是,每次我尝试启动应用程序时,它都会立即崩溃。错误说明:
2015-08-19 10:30:23.966 Venddy[3451:95841] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
如果有人能给我建议,我将不胜感激。我是应用程序开发的新手。谢谢!
答案
你应该在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
let userDefaults = NSUserDefaults.standardUserDefaults()
if let login = userDefaults.objectForKey("loginSuccess") as! Bool //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
}
希望这会有所帮助!
另一答案
你可以这样做
您只需将LoginSignup视图控制器添加到故事板,然后将“Storyboard Id”设置为“loginVC”。之后,在application:didFinishLaunchingWithOptions中检查用户是否登录。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let logined = ?
var vc: UIViewController?
if !logined {
vc = storyboard.instantiateViewControllerWithIdentifier("loginVC")
} else {
vc = storyboard.instantiateInitialViewController()
}
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
不要忘记在“部署信息”中的目标设置中删除“主界面”的值
另一答案
代码为Swift 4.2和5代码:
self.window = UIWindow(frame: UIScreen.main.bounds)
if(loggedIn){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "dashboardVC")
} else {
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "loginVC")
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
以上是关于如何在成功登录后更改初始视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章