Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from view controller to tab bar controller in iOS

I'm developing an iOS app and now I'm at a loss. I'm trying to pass data from the first View Controller to the first tab of a TabBarViewController (with using the storyboard). I found a lot of tutorials that explain how to pass data between view controllers, but nothing worked with my tab bar. I know that the tab bar controller contains a kind of array of views. The relation between the View Controller and the Tab Bar Controller is realized using a segue (push). So, I thought it is easy to use the prepareForSegue() - method. Like that:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
 if ([[segue identifier] isEqualToString:@"tab"]) {

 // ...

    } 
 }  

Unfortunately, the relation between the Tab Bar Controller and each tab bar view is no real segue. It's only a "relationship". That means, there is no segue identifier I am able to use for the the method above-mentioned. Is there a possibility to use the prepareForSegue in this case? If not, any ideas how to solve this problem? I know that there is a similar question, but the answer wasn't that helpful. Do I have to create a new file for every tab (view) within the tab bar controller? Or is it possible to have one class (m. & h.) for the whole tab bar controller, accessing the several view with objectAtIndex()?

Thanks in advance!

like image 316
Keks Avatar asked Jul 19 '13 09:07

Keks


People also ask

How do I add items to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


2 Answers

Here's my setup that worked:

  1. Setup Segue:
    1. Setup View Controller with segue to Tab Bar Controller with 2 child View Controllers in Storyboard
    2. Specify segue identifier (tab)
  2. Setup Classes in Storyboard:
    1. View Controller class = ViewController
    2. Tab Bar Controller class = TabBarController
    3. Tab Bar Controller Child View Controller class = TabsViewController (shared between both)
  3. Setup labelString property in Tab Bar Controller:

    1. In TabBarController.h:

      @property (nonatomic, strong) NSString *labelString;
      
    2. In TabBarController.m:

      @synthesize labelString;
      
  4. Setup prepareForSegue method in ViewController.m:

    #import "TabBarController.h"
    
    ...
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if ([[segue identifier] isEqualToString:@"tab"]){
            TabBarController *tabBar = [segue destinationViewController];
            [tabBar setLabelString:[NSString stringWithFormat:@"This has been set"]];
        }
    }
    
  5. Setup UILabels for Child Tab Bar View Controllers.

    1. Drag default UILabel controls into both child View Controllers
    2. Create property in TabsViewController.h:

      @property (nonatomic, strong) IBOutlet UILabel *label;
      
    3. Hook 5.1 and 5.2 up in Storyboard

  6. Setup ViewDidLoad method in TabsViewController.m:

    #import "TabBarController.h"
    
    ...
    
    @synthesize label;
    
    ...
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        TabBarController *tabBar = (TabBarController *)self.tabBarController;
        label.text = [NSString stringWithFormat:@"Tab %i: %@",[tabBar.viewControllers indexOfObject:self],tabBar.labelString];
    }
    

Now clicking on the 1st and 2nd tabs will have the labels display Tab 0: This has been set and Tab 1: This has been set, respectively.

like image 78
Yazid Avatar answered Sep 25 '22 02:09

Yazid


If your hierarchy is Viewcontroller->UItabbarcontroller->ViewCOntroller

in my case i have to send data to marketviewcontroller . In tabcontroller, marketviewcontroller is present at index 0.

  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
 if ([[segue identifier] isEqualToString:@"tab"])
  {
    UITabBarController *tabar=segue.destinationViewController;
    MarketViewController *marketViewcontroller=[tabar.viewControllers objectAtIndex:0];

   // pass data to market view controller
    [marketViewcontroller passobject:Yourdata];
 //   or 
  marketViewcontroller.value=Yourdata
  } 
  }  

in MarketViewController.h 
 @property(nonatomic,retain) NSString * value;
like image 43
Kalpesh Avatar answered Sep 27 '22 02:09

Kalpesh