0
votes

long time listener, first time caller.

I'm working on an app that consists of a login page (View Controller) and then dismisses to reveal a tab bar controller with 4 tabs, each with a Navigation Controller with their own views.

I'm looking to allow a user to click a "Logout" button that dismisses all the views associated with the Tab Bar Controller, segues to the Login View Controller, and then instantiates a new Tab Bar Controller. Without dismissing and creating a new Tab Bar Controller, all of the Tab views hold the old user data, since they are populated with "ViewDidLoad" functions. Unless there is another way to clear and trigger the "ViewDIdLoad" functions without dismissing all of the views/tab bar controller.

I'm open to other solutions as well since I'm very new to IOS Swift programming and am self taught.

My structure looks like this (note that the app enters the Tab Bar Controller first, if no current user is found it kicks back to the Login View Controller which dismisses itself upon login):Login/Tab Bar Controller relationship

2
when user logout then you have to set rootviewcontroller programically and take one bool for check user login or not so you can easily manage your tabbarHimanshu Moradiya
@HimanshuMoradiya Thank you for the suggestion. I think that is what gabriel suggested below. Im just not sure if this automatically dismisses the first view controller (so that it's not still lingering somewhere) and if that setting a new rootviewcontroller is a good practice. I'm going to implement the solution and report back.Xion
your problem solve or not ?Himanshu Moradiya
@HimanshuMoradiya Just implemented your solution as described also by Gabriel below. It appears to function exactly the way I need it too. Thank you!Xion

2 Answers

2
votes

The storyboard looks a bit confusing. I asume the app loads the Tab Bar the first and then, the login view controller is presented modally. But then, what is the segue from Login to Tab Bar? a push segue? I suppose it is not used and the login, as you say, is just dismissed.

To destroy the Tab Bar and load a new one from the storyboard you cannot use segues. It's even easier. Just have to assign a new instance of Tab Bar to rootViewController of the window of your app.

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window!.rootViewController = theNewTabBar

And to instantiate that "theNewTabBar", you will need to set an identifier in the storyboard and use

storyboard instantiateViewControllerWithIdentifier:
0
votes

You can Store Flag to check whether user is Logged in or not. On application loading time You Should open first LOGIN PAGE. BUT in LOGIN PAGE you have to check That flag.

If it is true than page Should directly Redirect to TAB BAR. If it is False than on Successful login you Should Set Flag True. At log OUT you Should POP TAbBAR and set Flag False.

This is Better Structure. For setting flag with application scope you should store bool is user default..This is function to store flag and get flag.

//for set Bool
class func setUserDefaultBool (value : Bool, key : String){
       UserDefaults.standard.set(value, forKey: key)
       UserDefaults.standard.synchronize()
}

//for get 
class func getUserDefaultBOOLForKey (key: String) -> Bool{
      return UserDefaults.standard.bool(forKey: key)
}