0

I have a TabBarController with 4 tabs setup in storboard editor. When I select the 4th tab, I want to send a string to its view controller. In my TabbarController class (named TabBarVC) I implemented the following method:

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    if(indexOfTab == 3) {
        SupplierListVC *slvc = (SupplierListVC *) viewController;
        slvc.locationType = @"favorite";
        self.tabBarController.selectedViewController = slvc;
    }
}

This method is being called just fine but the string is not getting passed. When I debugged, I noticed that the above piece of code is called after viewDidLoad of SupplierListVC. What am I doing wrong here? How can I pass the string when I select the tab?

5
  • are you instantiating the SupplierListVC anywhere else in your code?
    – katzenhut
    May 27, 2014 at 23:05
  • no I am not instantiating it. However, as I mentioned, its connected to the TabbarController in storyboard, so its getting displayed before didSelectViewController. May 27, 2014 at 23:08
  • disregard previous comment. have you checked that your cast is actually working? try NSLog("%s", NSStringFromClass(slvc.class)");after the cast and post the result.
    – katzenhut
    May 27, 2014 at 23:10
  • There's nothing wrong with what he's doing. The variable is being properly set. He's not checking it at the right time. viewDidLoad will ALWAYS be called on a vc in a tab bar BEFORE this method every has a chance to fire and for as long as that vc remains in the tab bar, its viewDidLoad will never be called again.
    – nhgrif
    May 27, 2014 at 23:12
  • @nhgrif - you are right. great answer, too.
    – katzenhut
    May 27, 2014 at 23:13

2 Answers 2

3

As you said, this method is called after viewDidLoad. The method name itself should tell you that: **DID**SelectViewController

Past tense. English may not be your first language, but a good understanding of English verb tense goes a really, really, really, really, really long way when working with Apple's method names. They're not misleading. They tell you exactly when/what/why that method is for, just in the method name.

Now then, the string SHOULD be appropriately passed into the view controller--you're probably just checking it at the wrong time.

But with a tab bar, the view controllers it contains aren't loaded each time that tab is switched to. The tabs are preloaded once and will only ever be unloaded if the tab bar controller itself is dismissed (or perhaps the view controller is removed from the tab bar controller's view controllers array).

You can use tabBarController:shouldSelectViewController:

This method, like the one you're using, gives you a reference to the tab bar controller and the view controller it's trying to switch too. It returns a BOOL value however. If you return NO the switch won't happen. You can simply always return YES if you want, the main point here though is that this method is called before the switch starts and therefore definitely before viewWillAppear and viewDidAppear are called.

1
  • Thanks, its 4am here and your great answer opened my eyes and my sleepy mind. May 27, 2014 at 23:16
0

I had the same issue when passing data from tabbar controller to viewcontroller.

NumberpadViewController *numpad= [[self viewControllers] objectAtIndex:1];
numpad.number=@"12345";

I had get number as nil in viewdidload method.

- (void)viewDidLoad {
 NSLog(@"number@",number);//nil value
    }

So I had used viewdidappear method to perfom processing on the number data.

-(void)viewDidAppear:(BOOL)animated{
   NSLog(@"number@",number);//12345
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.