我正在编写我的应用中的退出按钮。这是我应用的流程。
SplashvwController
- > secretCodeViewController
- >
LoginviewController
- > DashboardViewController
在此DashboardViewController
我有signOut按钮。
一旦用户登录,我的应用就会有单点登录功能,下次打开应用时
SplashvwController
- > DashboardViewController
我想在用户点击退出按钮时提示用户Loginviewcontroller
。
问题
当用户浏览路径1时,我只需执行popviewcontroller
即可返回上一个viewcontroller。但是,当用户通过第二条路径时,
Loginviewcontroller
添加到我的
viewcontrollers stack执行相同的操作?请帮帮我
答案 0 :(得分:0)
我认为,以下代码可以帮助您,
for (var i = 0; i < self.navigationController?.viewControllers.count; i++) {
if(self.navigationController?.viewControllers[i].isKindOfClass(Loginviewcontroller) == true) {
self.navigationController?.popToViewController(self.navigationController!.viewControllers[i], animated: true)
break;
}
}
要手动添加ViewController,请检查以下代码。
let navigation = mainStoryboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let nav = UINavigationController(rootViewController: navigation)
appdelegate.window!.rootViewController = nav
答案 1 :(得分:0)
在identifier
文件上为LoginViewController
设置情节提要参考Main.Storyboard
。
每当您想要显示LoginViewController
时,只需调用函数
func launchLoginView() {
if let loginVC: LoginViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginStoryboardID") as? LoginViewController {
// .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it
// LoginStoryboardID is the reference id for login view controller.
self.presentViewController(loginVC, animated: true, completion: nil).
// OR
//UIApplication.shared.keyWindow?.rootViewController = loginVC
}
}
根据用户登录状态登陆屏幕。
在AppDelegte.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isLoggedAlready = //Get the login status
if isLoggedAlready == true {
if let dashBoardVC: DashboardViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DashboardStoryboardID") as? DashboardViewController {
//Already logged in then directly launch dashboard vc.
//change the code based on your needs
window?.rootViewController = dasbBoardVC
}
}
// Otherwise let it go as flow 1
return true
}
}