Transferring data from a view controller to a tab bar controller in iOS

I am developing an iOS application and now I am at a loss. I am trying to transfer data from the first View Controller to the first TabBarViewController tab (using the storyboard). I found many tutorials that explain how to transfer 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 connection between the view controller and the tab bar controller is implemented using segue (push). So, I thought it was easy to use the prepareForSegue () method. For example:

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

Unfortunately, the link between the tab bar controller and each view of the tab bar is not a real step. This is just a "relationship." This means that there is no segue identifier that I can use for the above method. Is it possible to use prepareForSegue in this case? If not, have any ideas how to solve this problem? I know there is a similar question, but the answer was not so helpful. Should I create a new file for each tab (view) in the controller of the tab bar? Or is it possible to have one class (m. And h.) For the entire controller of the tab bar, access to multiple views using the AtIndex () object?

Thanks in advance!

+8
ios objective-c storyboard viewcontroller tabbarcontroller
source share
6 answers

Here are my settings that worked:

  • Segue setup:
    • Setup View Controller with segue to Tab Bar Controller with 2 storyboard child View controllers
    • Specify segue identifier ( tab )
  • Storyboard installation classes:
    • View Controller Class = ViewController
    • Tab Bar Controller = TabBarController
    • Tab bar controller TabsViewController view controller = TabsViewController (shared between them)
  • Setting the labelString property in the tab bar controller:

    • In TabBarController.h :

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

       @synthesize labelString; 
  • Setting up the 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"]]; } } 
  • Configure UILabel for tab bar controllers for children.

    • Drag and UILabel Controls to UILabel View Controllers by Default
    • Create a property in TabsViewController.h :

       @property (nonatomic, strong) IBOutlet UILabel *label; 
    • Hook 5.1 and 5.2 up in the storyboard

  • Setting the 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 display the labels Tab 0: This has been set and Tab 1: This has been set respectively.

+8
source share

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; 
+8
source share

How do you show your TabBarViewController from your UIViewController?

I guess using segue. If you do this, you can pass data to this UITabBarController, which is the "parent" for all controllers within the tabs.

Suppose you want to pass a string to a UITabBarController, you must define a property in this controller and set it before segue.

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

Then, using 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 } 
+1
source share

Look at singles. Inside the tab, you create an instance of the helper class that implements the singleton pattern, ensuring that only one instance actually exists. This means that when the second tab creates an instance of the same helper class, it will have access to the same object that you can use to exchange your data ...

for example: http://www.galloway.me.uk/tutorials/singleton-classes/

+1
source share

First:

1- Do you want to transfer data to your tab for some reason ...

or

2- Do you want to transfer data to your tab, then to switch to another UIViewController ?

If this is the second option, the most common template for it is Singleton. Where you can store general data that can be used / reused by different parts of the application.

0
source share

The easiest way is to create your own class and set the value from the controller to the user class, and then get the value from the user class to the controller.

Sample code here:

First create a custom class like

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 custom class into controller

 #import "AppUtility.h" 

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

 [AppUtility setXxxName:@"xyz"]; 

Second .m controller

import custom class into controller

 #import "AppUtility.h" 

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

 NSString *str = [AppUtility getXxxName]; 

In the appUtility class Use nslog statements to check if values ​​are correct or not

0
source share

All Articles