1

I have an app where once the user logs in, they stay logged in until logged out. once logged in, some style aspects like 'name' are presented for the user to see. My problem is that when I am logged in as 'user1' and then I log out and log in as 'user2' the view still presents 'user1' to the user. This is because the text containing 'user1' is set in viewdidload() on my TabBarController and when I logout/in, viewDidLoad() is not called again so the text remains the same. Is there some function or way to call viewDidLoad() on all of my ViewControllers once I Login again?

I tried ViewDidAppear(…) but I don't really want to be calling to the database to receive the name every time the user clicks the VC

Dane
  • 45
  • 6
  • You should update your view in ViewWillAppear/ViewDidAppear and load that data when the user logs in. If you're combining both the loading and displaying, you're doing it wrong. Look into caching and promise libraries – Malik Jun 19 '19 at 03:25
  • Possible duplicate of [Resetting Storyboard on Logout](https://stackoverflow.com/questions/10273910/resetting-storyboard-on-logout) – Mantu Jun 19 '19 at 04:48
  • Dane, what transitions do you have between logged in and logged out states? Is it a push/pop? Do you use storyboards? Do you use segues? Calling [viewController view]; would call the entire lifecycle again - including viewDidLoad, but that's not recommended at all, I can guide you on how you can do this without breaking the rules. – Mihai Erős Jun 19 '19 at 06:14

2 Answers2

1

i think you are going to login . page from tabbar navigationcontroller . loginpage should not be in that hierarchy . 1 . you can still make it possible by using viewwillappear 2. we can make it in view didload and this is recommended approach . make separate navigation controller for loginpage and set the window rootviewcontroller as per required business flow . so that once u log out ,change the wiindow,s rootviewcontroller so that entire tabbar controller will be removed from memory and then u login , change the root view controller to tabbarcontroller , now viewdidload will be called

Fisky
  • 114
  • 5
  • what code would You use for the goToLogin() function? I used window.rootviewcontroller = ..... and It would run the code of the other vc but not present it in the UI. – Dane Jun 19 '19 at 06:11
0

You can use NotificationCenter and send custom notification "login".
If some object (for example VC) depend User, that VC need subscribe this notification and update state.
Documentation
https://developer.apple.com/documentation/foundation/notificationcenter
Simple example
https://medium.com/@JoyceMatos/using-nsnotificationcenter-in-swift-eb70cf0b60fc

Other way reset navigation stack, e.g.

window.rootViewController = someNewNavigationController
Eysner
  • 584
  • 4
  • 15