Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "reset" the tabbar in an iPhone application

Tags:

iphone

tabbar

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.

like image 881
Joaquin McCoy Avatar asked Dec 03 '10 05:12

Joaquin McCoy


1 Answers

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.

like image 132
Matthew Frederick Avatar answered Oct 20 '22 13:10

Matthew Frederick