ios在导航控制器之间传递数据

我的应用程序有点问题,下面是这些视图控制器:

在这里输入图像说明

我有位置视图控制器的位置数组,我有一个位置数组在菜单表视图控制器,我需要传递数组从地点到菜单表,但我不知道如何,有人可以帮助我吗? 我已经尝试下面的代码:

-(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]; } 

你可以尝试这样的事情:

 - (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]; } 

您正在使用两个不同的MenuTableViewController对象。 你可以使用alloc / init创build一个,并给定位置,并从导航控制器获得另一个,然后按下。 您需要将数据传递给您要显示的控制器,而不是临时制作的控制器。

如何使用只有一个导航控制器,并通过使用segues视图控制器?