ios Pass data from TabbarController to ViewController in Storyboard

家住魔仙堡 提交于 2020-01-03 05:44:07

问题


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?


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/23900284/ios-pass-data-from-tabbarcontroller-to-viewcontroller-in-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!