Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instantiateViewControllerWithIdentifier and pass data

I am using Storyboard in my app and I want to pass data from one view to another view.

Instead of using segues I am using instantiateViewControllerWithIdentifier. In this case I am instantiate from my first TableViewController to a NavigationController which has a second TableViewController attached because I need the navigation in the second TableViewController. Now I want to pass data from my first TableviewController, depending which row was clicked, to my second TableviewController. In this case newTopViewController would be my NavigationController but my problem is now how to pass data from firstTableViewController to the secondTableviewController.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *identifier = [NSString stringWithFormat:@"%@Top", [menuArray objectAtIndex:indexPath.row]];


        UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];




    }

enter image description here

like image 470
halloway4b Avatar asked Apr 05 '13 11:04

halloway4b


2 Answers

If you instantiate a navigationController, you can use the viewControllers property to get the inner viewController of the navigation controller.

Something like this:

UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
MBFancyViewController *viewController = navigationController.viewControllers[0];

// setup "inner" view controller
viewController.foo = bar;

[self presentViewController:navigationController animated:YES completion:nil];
like image 146
Matthias Bauch Avatar answered Oct 13 '22 19:10

Matthias Bauch


newTopViewController.anyVariableToShow= anyVariableToSend;

I do this pretty often on a few of my apps...

//Create new VC

CookViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CookVC"];

//Set recipe
[detailViewController setRecipe:recipe];

//Pop over VC (can be pushed with a nav controller)
[self  presentPopupViewController:detailViewController animationType:MJPopupViewAnimationFade];

If you aren't using a navigation controller or segues, then I think you need to reconsider your app design.

like image 33
William Falcon Avatar answered Oct 13 '22 20:10

William Falcon