
我的故事板
如果用户未登录,则rootViewController为Login
用户登录完成后,rootViewController为MainTabBarController
我已经做到了
但是,我遇到的问题是Logout
我的Logout是dismissViewController
如果我的rootViewController是Login,它就可以工作
它将删除当前ViewController,因此会出现Login
但是当我的rootViewController为MainTabBarController时,驳回是不起作用的,我尝试过使用poptoRootViewController,但都是徒劳的。
我应该在Logout中做什么?
我想这样做
dismissController(true,{
rootViewController = `Login`
})发布于 2016-06-13 13:58:17
对于注销,执行以下操作:-(在调用注销的IBAction或didSelect ..etc方法中添加以下代码)
// Making Login as rootViewController as user is no longer logged in
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("Login") as! loginViewController
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDel.window?.rootViewController = loginVC另外,在AppDelegate中添加以下内容:
// Checking user login status, if user already logged in then making main tab bar view controller as root view controller
let userLoginStatus = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
if(userLoginStatus)
{
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
window!.rootViewController = centerVC
window!.makeKeyAndVisible()
}在验证用户凭据之后,还要在哪里进行登录验证:-
@IBAction func loginTapped(sender: AnyObject) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
// Important to set status to true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
appDel.window!.rootViewController = centerVC
appDel.window!.makeKeyAndVisible()
}备注:-不要忘记为所需的视图控制器添加情节提要ID,以激发它们
发布于 2016-06-13 13:01:51
您只需要在AppDelegate中创建一个函数,并在注销时调用该函数。
例如(Obj-C版本)
- (void) setCurrentRootController : (UIViewController *)viewController {
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:viewController];
[[[UIApplication sharedApplication].delegate window] setRootViewController:navigation];
}注销时,只需要设置rootViewController,然后调用popToRootViewController方法即可。
希望这对你有用!!
发布于 2016-06-13 13:02:31
你不需要使用任何额外的技术来在运行时设置rootViewController,除了以下几点:
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;如果你愿意,你可以用动画来包装它
https://stackoverflow.com/questions/37782001
复制相似问题