Cómo pasar el controlador de vista de datos a través del controlador de la barra de pestañas

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.
El código está aquí.

(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.


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.

preguntado el 31 de enero de 12 a las 16:01

Add your solution as an answer then you can accept it and get some points for it. -

en lugar de customizableViewControllers, Deberías usar viewControllers. customizableViewControllers only return view controllers that are re-arrangable. Should one view controller become unarrangable, you might get some unintended behavior. -

cuando pongo UITabBarController* tbc = [segue destinationViewController]; in, it says it doesn't like 'segue'. -

2 Respuestas

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

Respondido 20 Feb 14, 05:02

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 y TabBarController classes and connected them with storyboard.

Esto funcionó para mí:

First lets send data from ViewController a TabBarController:

  1. add segue to connect ViewController y 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. agregar esto a ViewController1.m, ViewController2.m, ViewController3.m

    @synthesize name;

  3. ir a tu TabBarController.m y agrega esto en ViewDidLoad método:

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

respondido 20 nov., 14:17

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.