12

I want to pass datum from view controller to view controller via tab bar controller.

I mean, my storyboard is like this.

View Controller → Tab Bar Controller → VC1/VC2/VC3

For the purpose of this, I tried to use PrepareforSegue, but it's failed.
The code is here.

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
vc = (friendsDetailViewController*)[segue destinationViewController];
 
vc.candidateid = [[NSString alloc] init];
vc.candidateid = [candidateid objectAtIndex:selectedIndexPath.row];

Though I was trying to pass data from VC to VC1, SIGABT happened on the last line.


Update:

I've got figured out how. Here is the script:

friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (friendsDetailViewController*)[[tbc customizableViewControllers] objectAtIndex:0];

Firstly, I have to attach Tab Bar Controller.
Secondly, retrieve each View Controller's pointer.
You can select by using objectIndex.

3
  • 2
    Add your solution as an answer then you can accept it and get some points for it.
    – McGafter
    Nov 15, 2012 at 14:47
  • instead of customizableViewControllers, you should use viewControllers. customizableViewControllers only return view controllers that are re-arrangable. Should one view controller become unarrangable, you might get some unintended behavior.
    – ninjaneer
    Dec 12, 2012 at 5:57
  • when I put UITabBarController* tbc = [segue destinationViewController]; in, it says it doesn't like 'segue'.
    – Grymjack
    Jan 2, 2013 at 6:14

2 Answers 2

0

In childviewcontrollers of tab bar controllers you can easily pass data by using

[self.parentViewController.view setAccessibilityValue:@"your data"];

and can easily access this data in other child viewcontrollers of tabbar

or you can add an object in nsuserdefaults for some key and attach that key in parentviewcontroller.view .. get that object by using objectforkey from nsuserdefaults in next viewcontroller

0

I had same problem. I had NSString which i waned to be sent like this:

View Controller → Tab Bar Controller → VC1/VC2/VC3

Before anything else I created ViewController-s and TabBarController classes and connected them with storyboard.

This worked for me:

First lets send data from ViewController to TabBarController:

  1. add segue to connect ViewController and TabBarController in storyboard and name it: toMyTabController
  2. add property u want to change in TabBarController:

    @property (strong, nonatomic) NSString *name;

  3. add this method somewhere in ViewController.m:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"toMyTabController"]) { MyTabController *tabController = (MyTabController *)segue.destinationViewController; tabController.name = @"SOME AWESOME NAME"; } }

================================================================================== Now lets send data from TabBarController to ViewController

  1. Add property to every ViewController where you want to send info, and lets name it name (so creative) add this to **ViewController1.h, ViewController2.h, ViewController3.h

    @property (strong, nonatomic) NSString *name;

  2. add this to ViewController1.m, ViewController2.m, ViewController3.m

    @synthesize name;

  3. go to your TabBarController.m and add this in ViewDidLoad method:

for (UIViewController* myController in self.viewControllers) { if([myController respondsToSelector:@selector(setName:)]) { [myController performSelector:@selector(setName:) withObject:self.name]; } }

Thats all, Hope it helps. Good Luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.