简体   繁体   中英

ios Pass data from TabbarController to ViewController in Storyboard

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?

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM