How can I “reset” the tabbar in an iPhone application

耗尽温柔 提交于 2019-12-04 03:15:11

For login-logout-login situations where all kinds of things need to reset themselves at the logout or the next login, I like to create a notification, something like "NewUserReset." Everything that needs to reset itself to an original state listens for the notification and runs a method that does whatever kind of resetting it needs. The tabbar would change the button title to logout, temporary data structures nil/zero/release themselves, etc.

It's nicely decouples the logout from all of the things that have to be done so you're not trying to manipulate view controllers and data storage and view appearances from the the controller that received the logout tap.

Sending a notification is easy. When the user taps the Logout button you'll send out a notification like this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"JMUserLogout" 
                                                object:nil];

You don't have to call it JMUserLogout, you just need a string that you'll recognize and something -- I used your initials -- to help ensure you don't accidentally send a notification that has the same name as a notification something you're unaware of is listening for.

When that notification goes out, any object that has registered with the defaultCenter to listen for @"JMUserLogout" will perform any actions you choose. Here's how your object registers (this should be located in some place like ViewWillLoad or the initialization method of the object):

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

The selector there, resetForNewUser:, is just the name of a method you want to run when the notification goes out. That method looks like this:

- (void)resetForNewUser:(NSNotification *)notif {
    // DO SOMETHING HERE
}

Where it says // DO SOMETHING HERE you'll add the code specific to your app. For example, you can add the tab bar as an observer of the JMUserLogout notification. In its resetForNewUser: method you'd change the name of the logout button to Login.

In a ViewController or View or data store that holds old data from the previous user the resetForNewUser method would delete all of that data and set things back to the way they should be fore a new user. For example, if the previous user entered data into a UITextField you would delete the text, yourTextFieldName.text = @"";

Lastly, it's important that you also remove your object as an observer before it's deallocated. In your Dealloc method of each object that registered to receive the notification you add this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Hopefully that makes sense. The Apple documentation for NSNotificationCenter explains more and they provide several sample apps that use notifications.

Seems like tabBarController is not getting released. [ retain count should be 1 before releasing] tabBarController might be retain somewhere. check the retain count of it.

If you want to reset the old data from the previous user after you log 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.

Satya

The tabBarController object may have been retained somewhere. Try to remove that.

And use the following code for Login, Logout methods

- (void)loginComplete {

    // initialize the tabBarController here. like the following
    if(tabBarController == nil){
    tabBarController = [[UITabBarController alloc] init];

    }
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

- (void)logoutComplete {
    [[tabBarController view] removeFromSuperview];
    [tabBarController release];
    tabBarController = nil;
    [window addSubview:loginView.view];
}

So that your problem will be solved.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!