1

I'm having a bit of a problem with my app, I have these view controllers below:

enter image description here

I have the locations view controller with an array of locations and I have a locations array in the Menu Table view controller, I need to pass the array from Locations to Menu Table, but I dont know how, can somebody Help me? I have tried this coding below:

-(void)pushMenuTableView
{

UIStoryboard *storyboard = self.storyboard;
UINavigationController *menuTableController = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"MenuTableViewController"];

MenuTableViewController *menuController = (MenuTableViewController *) menuTableController.topViewController;

[self.navigationController pushViewController:menuTableController animated:YES];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuTableViewController *mvc = [[MenuTableViewController alloc]init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Location *selectedLocation = [_locations objectAtIndex:[path row]];
[mvc setLocation:selectedLocation];
[self pushMenuTableView];

}

2 Answers2

2

You are using two different MenuTableViewController objects. One you create using alloc/init and give the Location to and another that you obtain from the navigation controller and then push. You need to pass the data to the controller you're going to display instead of one you make temporarily.

How about using only one navigation controller and moving through the view controllers using segues?

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
1

You could try something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = self.storyboard;
    MenuTableViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MenuTableViewController"];

    Location *selectedLocation = [_locations objectAtIndex:indexPath.row];

    vc.location = selectedLocation;

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

    [self presentViewController:nc animated:YES completion:nil];
}
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Hmm - how would you do this if the navigation controller had 2 view controllers, and you wanted to pass Location to each of them? – mliu Jan 16 '15 at 20:48