Passing data from view controller to tab bar controller in iOS

余生颓废 提交于 2019-12-03 06:55:03

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.

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;

How are you showing your TabBarViewController from your UIViewController?

I am guessing with a segue. If this is the way you're doing it, you can pass data to this UITabBarController which is the "parent" for all the controllers inside the tabs.

Say you want to pass a string to the UITabBarController, you would define a property in this controller and set it before the segue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
      if ([[segue identifier] isEqualToString:@"tab"])
      {
          UITabBarController *tabBarVC=segue.destinationViewController;
          tabBarVC.stringToSet = @"hi";
      }
  }

Then, with the delegate method, you have the selected view controller, so you can pass the parent property to the children:

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    viewController.stringToSet = self.stringToSet // self is UITabBarController
}

Have a look at singletons. Inside a tab you create an instance of a helper class which implements the singleton template, ensuring that there is actually only one single instance. Which means when the second tab instantiates the same helper class it will have access to the same object, which you can use to share your data ...

e.g.: http://www.galloway.me.uk/tutorials/singleton-classes/

First thing:

1- You want to pass the data to your tab for some reason...

or:

2- You want to pass the data to your tab so then you can pass to another UIViewController?

If it's the second option, the most common pattern for that is the Singleton. Where you can keep common data, that can be used/reused by different parts of the application.

The easiest way is create a custom class and set the value from controller to custom class and then get the value from custom class to the controller.

Sample code is here:

First create custom class for example

AppUtility.h

+(void)setXxxName:(NSString *)str;
+(NSString *)getXxxName;

AppUtility.m

static NSString *xxxname;

+(void)setXxxName:(NSString *)str{

   xxxname=str;
 }

+(NSString *)getXxxName{

  return xxxname;
 } 

FirstController.m

import the custom class in the controller

#import "AppUtility.h"

set the value from first controller to custom class, use the below code to set the value:

[AppUtility setXxxName:@"xyz"];

Second controller.m

import the custom class in the controller

#import "AppUtility.h"

here get the value from the custom class, use the below code to get the value:

NSString *str = [AppUtility getXxxName];

In appUtility class Use nslog statements to verify the values are correct or not

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