Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Nic Huang
PLUS
Nic Huang
Courses Plus Student 10,573 Points

Reload UITabbarController along with it's child view controller when log out

In my app, I have a login screen allows users to log out and and log in. What I want to know is that what's the best way to remove all data of the current user when he logs out ??

What I'm doing right now is manually remove his data when he logs out using NSNotification. But I want to know is there any way to reload the whole UITabbarController just like first time I initialize the app ??

Thanks in advance.

2 Answers

Could you perform a segue to the same view controller (there are ways) and set things up in viewWillAppear?

Stone Preston
Stone Preston
42,016 Points

you could use the NSNotification center. simply subscribe all your viewControllers in your tabBar to a notification and set it up so that they set their data sources to nil whenever a user logs out. then when the log out button is pressed, publish that notification.

so in each view controller in viewDidLoad subscribe to the notification:

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(logoutOccurred:) 
        name:@"Logout"
        object:nil];

and then add a method called logoutOccurred that sets the data source to nil:

- (void)logoutOccurred {

self.messages = nil;
}

When the logout button is pressed, you need to publish the logout notification:

//inside your logout button IBaction
[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"Logout" 
        object:self];

all your subscribing objects will be notified, and they will run whatever code you have specified in your selector when you first subscribed.

You need to be sure and unsubscribe your notifications. implement the dealloc method in each of your view controllers and add:

- (void) dealloc
{

 [super dealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

the prevents the center from sending messages to deallocated objects