cannyboy
cannyboy

Reputation: 24406

Re-initialiaze tabs in tabbar

I've got a tab bar set up in a storyboard with 4 tabs. The app has a login system, and when I log out I want to zero-out the second and third tabs. The second and third tabs content depends upon which user is logged in, so I don't want the old info appearing there.

How would I re-initialise them?

Upvotes: 9

Views: 426

Answers (8)

Charan Giri
Charan Giri

Reputation: 1097

I have got 2 options

  1. As you said tab 2 & 3 are showing based on user login. So for sure you will have userId of that user and you will be saving it some where like NSUserDefault. When you log out clear the userId and in 2 & 3 tabs load information if userId have some value.

  2. Clear UserId and make or force user to login in a soft way be using this

    [method] [self.navigationController popToRootViewControllerAnimated:YES];

on logout button action

Upvotes: 0

Martin
Martin

Reputation: 1135

I don't really use storyboards but in straight code this is a piece of cake.

In your AppDelegate.h create a property for tab 2 / tab 3 with the relevant view controllers.

@property (nonatomic,strong) MyViewController *tab2
@property (nonatomic,strong) MyOtherViewController *tab3

etc and use this to initialise the tabs - it will be delegate.tab2 or self.tab2 if you are in AppDelegate.m but globally accessible.

Off the top of my head you would initialise it like:

[self.tabBarController setViewControllers:@[tab1,self.tab2,self.tab3,tab4]];
[self.window setRootViewController:self.tabBarController];

Now, when the user logs out run an app delegate method that either resets the view controller or puts something in its place.

e.g. in AppDelegate.m

-(void)logout
{
    [self clearView];
    // the rest of hte method
}

-(void)clearView
{
    [self.tab2 resetData];
}

in MyViewController.m

-(void)resetData
{
    [textFieldOrWhateverTheUIElementIs setText:@""];
// and so on
}

Simple.

Upvotes: 1

Matthieu Rouif
Matthieu Rouif

Reputation: 2863

You can subclass your UITabViewController so it can listen to a NSNotification then in your UITabViewController why don't you create two new controllers to replace the old ones as below

NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithArray:_tabBarController.viewControllers];
SecondViewController * resetSecondViewController = [[SecondViewController alloc] init];
ThirdViewController * resetThirdViewController = [[ThirdViewController alloc] init];
[viewControllers replaceObjectAtIndex:2 withObject:resetSecondViewController];
[viewControllers replaceObjectAtIndex:3 withObject:resetThirdViewController];
[_tabBarController setViewControllers:viewControllers animated:NO];

Upvotes: 2

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

One way you can try using key value observing like this :- Whenver you click on the logout store some value in string which is NSString *selectedValue make the property and synthesize of the same. So whenever string value will change below observer will be notified

// Also add below in your view controller.

[yourBtn addObserver:self forKeyPath:@"contentViewController.selectedValue" options:NSKeyValueObservingOptionNew context:(__bridge void *)(self)];

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
   if ([keyPath isEqualToString:@"contentViewController.selectedValue"])
//your Code stuff here
}

Upvotes: 3

Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

You can try:

UITabBarController *tabBarController;
NSMutableArray *tabBarButtons = [tabBarController.tabBar.items mutableCopy];
[tabBarButtons removeObjectAtIndex:1];
[tabBarButtons removeObjectAtIndex:2];
[tabBarController.tabBar setItems:tabBarButtons animated:NO];

Upvotes: 3

iShameem
iShameem

Reputation: 101

So basically you want to disable the tow tabs. You can use delegate method of "UITabbarController" ,

   - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

And return NO, for which tab you want to make disable.

Upvotes: 2

Wain
Wain

Reputation: 119031

This is a situation suitable for NSNotification usage. When the 'logged-in' status changes, post a notification. Observe this notification in the appropriate view controllers and have then react by enabling / disabling / showing / clearing UI elements in their view.

Upvotes: 5

zbMax
zbMax

Reputation: 2818

With :

NSMutableArray *tabBarButtons = [tabBar.items mutableCopy];

you will have a mutable copy of the array representing your tabs. So with this copy, you can change your tabs properties, remove tabs if you want, add others,...

And to finish, perform a

[tabBar setItems:tabBarButtons animated:/*YES or NO*/];

to apply your changes to your tab bar

Upvotes: 2

Related Questions