问题描述
我已经在AppDelegate中将UIViewController设置为rootViewController,但是,当用户登录或跳过UIViewController时,我会在顶部显示一个UITabBarController.
I've set up a UIViewController as my rootViewController in AppDelegate, however, when a user logs in or skips it I am presenting a UITabBarController over the top.
我需要关闭LoginController,然后在用户登录后将UITabController设置为rootViewController.
I need to dismiss the LoginController, and set the UITabController as rootViewController instead after user logs in.
我该如何进行重组?
AppDelegate()
AppDelegate()
window = UIWindow()
window?.makeKeyAndVisible()
window?.rootViewController = LoginController()
LoginController()
LoginController()
self.present(MainTabBarController(), animated: true, completion: nil)
推荐答案
维护状态对于用户是否登录(然后根据该状态)很容易.您可以导航到视图.
It's too easy to maintain condition for user logged in or not, and then based on that status. You can navigate to your view.
您可以尝试这种方式:
1.在 AppDelegate
中,您可以使用这些方法.
1.In AppDelegate
you can these methods.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let isLogin = UserDefaults.standard.bool(forKey: "IS_LOGIN")
if isLogin == true {
self.goToDashboardView()
} else {
self.goToLoginView()
}
return true
}
//MARK:- ------- Global Methods -------
class func sharedInstance() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
func goToLoginView() {
let sb = UIStoryboard.init(name: "Main", bundle: nil)
let loginVC = sb.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let navVC = UINavigationController(rootViewController: loginVC) // You can skip this if you do not want to add navigation bar
self.window?.rootViewController = navVC // if you skipped above line, then you have to assign 'loginVC' here.
self.window?.makeKeyAndVisible()
}
func goToDashboardView() {
let sb = UIStoryboard.init(name: "Main", bundle: nil)
let tabbarVC = sb.instantiateViewController(withIdentifier: "MyTabBarController") as! MyTabBarController
self).window?.rootViewController = tabbarVC // if you skipped above line, then you have to assign 'loginVC' here.
self.window?.makeKeyAndVisible()
}
2.用户成功登录后,在 LoginViewController
中.
2.In LoginViewController
, when user logged in successfully.
@IBAction func btnLoginClicked(_ sender: UIButton) {
// Your API call or other code
// If all things goes well, then login and go to dashboard
UserDefaults.standard.set(true, forKey: "IS_LOGIN")
AppDelegate.sharedInstance().goToDashboardView()
}
3.最后,只要您想从应用程序中注销,无论何时何地,只需调用以下代码即可.
3.And finally, whenever and from wherever you want to log out from app, just call below code.
@IBAction func btnLogOutClicked(_ sender: UIButton) {
// Your API call or other code
// If all things goes well, then logout and go to login view
UserDefaults.standard.set(false, forKey: "IS_LOGIN")
AppDelegate.sharedInstance().goToLoginView()
}
4. Main.storyboard
的设计应类似于:
4.And Main.storyboard
should have design something like :
这篇关于在呈现新的UITabBarController之前如何关闭rootViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!