ios Pass data between navigation controller

久未见 提交于 2020-02-05 04:02:25

问题


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

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

}

回答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];
}



回答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?



来源:https://stackoverflow.com/questions/22699900/ios-pass-data-between-navigation-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!