开发者

How to reset an iOS application state after a user clicks sign out?

开发者 https://www.devze.com 2023-03-13 03:48 出处:网络
So I posted a question a while back which can be seen at: How to reset an iOS application when a user clicks the "sign out button"?

So I posted a question a while back which can be seen at: How to reset an iOS application when a user clicks the "sign out button"?

Following the advice I made a sign out button where a user where by is taken to the main screen where they can register or sign in again. What I am finding out is that when a new user signs in, I am seeing certain value from the old user in pickers, UITextView ETC

Is there a way to reset application state or do I have to go the long route of making sure that each outlet is set to default values? Is this a sign of bad programming practice somewhe开发者_如何学Pythonre?


The Cocoa way = KVO (key value observing). Controllers interested in being informed about login state change register themselves as observers on login component/controller/whatever instance does the login.

After login/logout this component notifies all observers about state change. Those then do all necessary actions: populating UI with user data after log in or resetting them after log out.

Very flexible pattern that avoids unnecessary dependencies between components.


Ok first thing you have to do is make your main view implement the delegate for signout successful. In this you got to reset all the data and views which will be recreated/repopulated when a new user signs in.

Inorder to achieve this you can analyse your code/logic as to what is created on a new user sign in and reset all these in the delegate method for sign out. This is way to generic an answer but resetting the data is the way to do it.

Alternatively you can recreate your main view when sign in is successful i.e. remove the main view and on sign in success create it afresh for the new user.


The cleanest way would be to create a new set of view controllers and set them to the viewControllers of the UITabBarController object but it needn't be the cheapest always. This will be something that you need to check if it's viable or not.

Otherwise, you'll have to to consider adding reset methods to the view controllers. If the tab is a navigation controller, popToRootViewControllerAnimated: and reset the first view controller. This one is a bit of effort to implement compared to the former approach.


When ever you go to new controller just allocate the whole controller again. and dont forget to release once the navigation is done.

Heres the sample code example to do it

-(void)goToFormController
{
  FormViewController *objFormViewController = [[FormViewController alloc]initWithNibName:@"FormViewController" bundle:nil];
  [self.navigationController pushViewController:objFormViewController animated:YES];    
  [objFormViewController release];
}

Happy iCoding...

0

精彩评论

暂无评论...
验证码 换一张
取 消