我想将视图控制器设置为初始视图控制器
I want to set a view controller as initial view controller
当用户已经登录时,应用程序应该从主页打开,he/she 不应再次进入登录页面。
我试过这段代码,它没有显示任何错误,但它不起作用。
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC =
storyboard.instantiateViewController(withIdentifier:
"initController") as! ViewController
self.window?.rootViewController = firstVC
self.window?.makeKeyAndVisible()
}
我希望当用户已经登录后,当她再次打开应用时 he/she 必须直接进入主页,不应再次进入登录页面。
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier:
"initController") as! HomeController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
super.viewDidLoad()
}
let rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "rootVcIdentifier") as! UINavigationController
self.view.window?.rootViewController = rootViewController
当用户已经登录时,应用程序应该从主页打开,he/she 不应再次进入登录页面。
我试过这段代码,它没有显示任何错误,但它不起作用。
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC =
storyboard.instantiateViewController(withIdentifier:
"initController") as! ViewController
self.window?.rootViewController = firstVC
self.window?.makeKeyAndVisible()
}
我希望当用户已经登录后,当她再次打开应用时 he/she 必须直接进入主页,不应再次进入登录页面。
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier:
"initController") as! HomeController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
super.viewDidLoad()
}
let rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "rootVcIdentifier") as! UINavigationController
self.view.window?.rootViewController = rootViewController