How can I "reset" use a tab in an iPhone application

I have an application for iPhone: When you open the application, you see "LoginView". If you go into the application, you see TabBarController. On the third and last tab there is a button "Exit". If you click, you will see "LoginView" again. My problem is that when you log in again, you see the "old" tab, and the selected tab is the third, and not the one, and the "Logout" button. In addition, if the user enters a username with another user, see Old data from 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 are two methods in two different view controllers:

 - (IBAction)login:(id)sender { TabNavisAppDelegate *delegate = (TabNavisAppDelegate *) [[UIApplication sharedApplication] delegate]; [delegate loginComplete]; } 

(exit method is the same)

Guys, how can I solve this painful problem? So, here is a list of apps that do what I want: Foursquare, Brightkite, and others. Each of them has a login screen, a tab view and a logout button.

Thanks @ everyone.

+8
iphone tabbar
source share
4 answers

For login situations, where all kinds of things must reset themselves when logging out or next logging in, I like to create a notification like "NewUserReset". All that is necessary for reset for the initial state listens for a notification and launches a method that performs any type of reset that it needs. On the tab, the name of the button to exit the system has changed, the temporary data structures are nil / zero / release, etc.

It perfectly separates the output from all the things that need to be done so that you do not try to manipulate the view controllers and data storage and display on the screen from the controller that got the system out.

Sending a notification is easy. When the user removes the Exit button, you will send a notification similar to this:

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

You do not need to call it JMUserLogout, you just need a line that you recognize, and something - I used your initials - to ensure that you do not accidentally send a notification with the same name as a notification that you do not know about, listening.

When this notification disappears, any object that registered with the center by default to listen to @JMUserLogout will perform any action you choose. Here's how your object is registered (this should be located in some place, such as ViewWillLoad or the object initialization method):

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

The selector there, resetForNewUser :, is simply the name of the method that you want to run when the notification is disabled. This method is as follows:

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

Where he says // DO HERE, you will add code specific to your application. For example, you can add a tab bar as a JMUserLogout notification observer. In your method resetForNewUser: you must change the name of the logout button to log in.

In the ViewController or View or data store, which stores old data from the previous user, the resetForNewUser method will delete all this data and return things as they should be in front of the new user. For example, if a previous user entered data in a UITextField, you would delete the text, yourTextFieldName.text = @ "";

Finally, it is important that you also remove your object as an observer before it is released. In your Dealloc method, for each object registered to receive a notification, you add the following:

 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

Hope this makes sense. The Apple documentation for NSNotificationCenter explains more, and they provide some sample applications that use notifications.

+9
source share

It seems that the tabBarController is not freed. [save account must be 1 before release] tabBarController can be saved somewhere. check its quantity.

0
source share

Perhaps the tabBarController object was saved somewhere. Try to remove this.

And use the following code for login methods, 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 is resolved.

0
source share

If you want to reset the old data from the previous user after logging out. All you have to do is reset the UITabBarController viewControllers property.

So, if you subclass UITabBarController, the following code should restore your application to its original state.

  self.viewControllers = @[self.viewControllerOne, self.viewControllerTwo, self.viewControllerThree]; 

From the documentation:

If you change the value of this property at run time, the tab bar controller will remove all old view controllers before setting new ones. Tab bar items for new view controllers are displayed immediately and are not animated to position.

0
source share

Source: https://habr.com/ru/post/649782/


All Articles