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

爷,独闯天下 提交于 2019-12-14 03:41:15

问题


I've an iPhone application: When you open the app you see the "LoginView". If you login into application you see a TabBarController. In the third and last tab there is "Logout" button. If you click you see the "LoginView" again. My problem is that if you login again you see the "old" tabbar and the selected tab is the third and not the one, and there is a "Logout" button. Also, if a user login with a different user, see the old data of the previous user (very dangerous).

Here's the code: - Delegate.h:

UITabBarController *tabBarController;
LoginViewController *loginView;

- Delegate.m (didFinishLaunchingWithOptions):

[self.window makeKeyAndVisible];

loginView = [[LoginViewController alloc] init];

if (YES) { /* if the user is not already logged */
    [self.window addSubview:loginView.view];
}

Delegate.m (methods):

- (void)loginComplete {
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

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

And here's the two methods in two different viewcontrollers:

- (IBAction)login:(id)sender {

     TabNavisAppDelegate *delegate =
      (TabNavisAppDelegate *) [[UIApplication sharedApplication] delegate];
  [delegate loginComplete];
  }

(the logout method is the same)

Guys, how can I solve this painful problem? So, here's a list of application that do what I want: "Foursquare", "Brightkite" and others. Each one have a login screen, a tabbar view and a logout button.

Thanks @ everyone.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4342742/how-can-i-reset-the-tabbar-in-an-iphone-application

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