iosxcodelogoutnavigationcontrollertabbarcontroller

restoring iOS app to original state and clearing all data when user logs out


i have an app with a signin viewController which modally presents a tabBarController containing several tabs. Each tab has a navigationController and a stack of views. One of those tabs is for settings and has a logout button.

when the user presses the logout button I would like to dismiss all navigation stacks of all tabs and the tabBarController and go back to the initial login viewController. Essentially I want to restore the app to the initial state. Was wondering what the best practice would be to achieve this.

thanks


Solution

  • If you want to reset all of the tabs and return the apps to it's initial state after logging out, all you have to do is reset the UITabBarController's viewControllers property.

    So if you are subclassing UITabBarController the following code should restore your app to its original state.

        self.viewControllers = @[self.viewControllerOne, self.viewControllerTwo, self.viewControllerThree];
    

    From the documentation:

    If you change the value of this property at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. The tab bar items for the new view controllers are displayed immediately and are not animated into position.