Data transfer to the tab controller

I have a storyboard project, and I would like to transfer some data from the view to the controller of the tab bar, the information will be scattered between the tabs. After some research, I found a very similar problem: iOS storyboard that passes navigationViewController data , but the only problem with this solution is that it did not transfer to the tab bar controller. I was wondering if I need to transfer data to each tab, or can I transfer it to the controller of the tab bar and then distribute it from there? Thanks for your help in advance!

I am currently using the following: but, I am getting an error:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"FoodPage"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; UINavigationController *nav = [segue destinationViewController]; FoodViewController *destViewController = (FoodViewController*) nav.topViewController; destViewController.Foods = [foodArray objectAtIndex:indexPath.row]; } } 

+6
source share
2 answers

To get a link to your UINavigationController in your case, you must do the following and first specify the correct tabBarController index:

 UINavigationController *nav = [self.tabBarController.viewControllers objectAtIndex:<THE_INDEX_NUMBER_FOR_YOUR_NAVIGATION_CONTROLLER]; 

Once you do this, you will get a link to your FoodViewController , again indicating the index number for it on the UINavigationController (i.e. if it is on top, and then 0 ):

 FoodViewController *destViewController = (FoodViewController*) [self.nav.viewControllers objectAtIndex:0]; 

Update

Now that I understand what you would like to achieve:

You are using the UITabbarController into which your other controllers are embedded.

Script / example example :

Let's say we had 2 view controllers, controller A and controller B, respectively, both built into the UITabbarController .

What do we want:

We are trying to change the text a UILabel in controller B from controller A.

First declare property in controller B in .h:

 @property(strong, nonatomic) UILabel *aLabelInControllerB; 

Second, declare a property (or ivar) in your controller A:

 @property(strong, nonatomic) ControllerB *controllerB; 

Since you are using a UITabbarController , you do not need to use segue, you can just get the UITabbarController through self.tabBarController ;

Question : "How do I know when my tab bar controller will listen, and then change the label text in controller B?"

We doing this:

Set Controller A as a UITabbarController delegate by:

In the controller Ah add:

 @interface <Your_Controller> : UIViewController <UITabBarControllerDelegate> 

In viewDidLoad controller A:

 self.tabBarController.delegate = self; 

And in the Am controller, we implement this method:

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { self.controllerB = (ControllerB *) [tabBarController.viewControllers objectAtIndex:1]; //In our example here, we only have 2 view controllers (A and B) //So, index 1 is where controller B resides. self.controllerB.aLabelInControllerB.text = @"Hello!"; //This will change the text of the label in controller B } 

And as controller B appears, you will see that the label text will be changed to "Hello!"

Installing a NSString in controller B follows the same procedure.

Example: self.controllerB.stringInControllerB = @"Hi from controller B!";

Hope this helps.



Update 2:

Go from table cell to tab bar controller? Okie .. Here is the solution. I use only one cell in my example, so if you want to have more cells in the future, I would leave it to you to set up.

Let's look at the layout:

In the control storyboard, drag a tab bar from your cell to the controller.

Add a UINavigationController as in the picture.

In UITableViewController .m:

Add 2 properties:

 @property (strong, nonatomic) UITabBarController *myTabbarController; @property (strong, nonatomic) YourFirstViewController *myFirstViewController; 

Just a friendly reminder:

Do not forget to add:

 self.tableView.delegate = self; self.tableView.dataSource = self; 

Add the following to the table view controller:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { self.myTabbarController = (UITabBarController*) [segue destinationViewController]; self.myFirstViewController = [self.myTabbarController.viewControllers objectAtIndex:0]; self.myFirstViewController.stringFromTableViewController = @"Hi from TableViewController!"; } 

And in myFirstViewController add 2 properties to .h:

 @property (strong, nonatomic) NSString *stringFromTableViewController; @property (strong, nonatomic) YourSecondViewController *secondViewController; 

As in the second of the above changes, your first view controller must still be a UITabBarControllerDelegate delegate and implement this method:

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { self.secondViewController = (YourSecondViewController*) viewController; self.secondViewController.aLabel.text = self.stringFromTableViewController; } 

As before, nothing needs to be changed in the SecondViewController .

I ran myself; It should be good to get to the setting.

Enjoy.

+6
source

You can also access data from the TabBar inside the TabBar child controllers themselves, for example:

 MyTabbarViewController *tabbar = (MyTabbarViewController *)self.tabBarController; NSLog(@"%@", tabbar.data); 

Thus, you only need to set the data in the TabBar, as shown above, and then access it when you need it, in the child view.

+2
source

All Articles