nine9
nine9

Reputation: 147

iOS how to change rootViewController when dismissController

enter image description here

my storyboard

If user doesn't login, the rootViewController is Login

after user login done, rootViewController is MainTabBarController

I have done that

But, I have encounter question is Logout

My Logout is dismissViewController

If my rootViewController is Login, it works

It will remove current ViewController, so Login appear

But when my rootViewController is MainTabBarController, dismiss is not work, I've try using poptoRootViewController in vain.

what should I do in Logout ?

I want to do like this

dismissController(true,{
   rootViewController = `Login`
})

Upvotes: 1

Views: 4593

Answers (6)

Sébastien REMY
Sébastien REMY

Reputation: 2470

Warning, make sure your are in the main thread if you use Delegate or Completion methods.

Code in Swift 3

DispatchQueue.main.async {
        guard let tb = self.storyboard?.instantiateViewController(withIdentifier: "LoggedUser") as? UITabBarController else {
            print("Could not instantiate view controller with identifier of type LoggedUser")
            return
        }
        self.present(tb, animated: true, completion: nil)}

Upvotes: 0

Guntikogula Dinesh
Guntikogula Dinesh

Reputation: 11

You can maintain two windows, one is for login, and second one for the users who got authenticated.

This way you can easily switch btw windows rather than having a messy MVC for it.

Upvotes: 0

Meet
Meet

Reputation: 1208

For Logout do following:- (Add below code inside IBAction or didSelect ..etc method where logout is called)

// 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

Also add following in 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()
            }

And Also where login Validation is done, after validating user credentials:-

 @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()
}

NOTE:- Don't forget to add STORYBOARD IDs for required View Controllers to instiantiate them

Upvotes: 5

Subin K Kuriakose
Subin K Kuriakose

Reputation: 849

In Swift You can do like this

let vc: UIViewController! = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController")

    let window = UIApplication.sharedApplication().windows[0];
    window.rootViewController = vc;

Upvotes: 0

Aleksandr Medvedev
Aleksandr Medvedev

Reputation: 9008

You don't need to use any additional technics to set rootViewController in runtime except following:

UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;

You can wrap this with animation if you want

Upvotes: 0

Mahendra
Mahendra

Reputation: 8924

You just need to make a function in AppDelegate and call that function on logout.

eg.(Obj-C Version)

- (void) setCurrentRootController : (UIViewController *)viewController {

    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:viewController];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:navigation];
}

When you are doing logout you just need to set rootViewController and then call popToRootViewController method.

Hope this will work for you !!

Upvotes: 1

Related Questions