简体   繁体   中英

UINavigationController with two copies of the same view controller on stack

I am using a UINavigationController to push a UIViewController onto the stack, then when the user selects a row in the table view I allocate a new version of the view controller and push it onto the stack this works fine but then when I start pressing the back button the NSMutableArray that I am using to store the information seems to be keeping the value from the view controller that was just poped from the stack.

Here is the method that is pushing the new onto the stack:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 1) {
        if (hasLoadedResponses && [responses count] == 0) {
            return;
        }

        VideoDetailViewController_iPhone *nextView = [[VideoDetailViewController_iPhone alloc] initWithNibName:@"VideoDetailViewController_iPhone" bundle:nil withVideoArray:[responses objectAtIndex:indexPath.row]];
        nextView.navController = navController;
        [navController pushViewController:nextView animated:YES];
        [nextView release];
    }

}

I'm sure there is something stupid I am missing, but can't seem to find it right now.

Assuming the mutable array is [responses objectAtIndex:indexPath.row] because is the only value you pass to the new pushed view controller, then if you are modifying it in you have to take in account that is the same object and so modifications are shared.

You can write:

. withVideoArray:[NSMutableArray arrayWithArray:[responses objectAtIndex:indexPath.row]];

As I said, I assume that object is the mutable array of your problem.

Another smarter solution is defining the property you use to store the mutable array as 'copy' instead of 'retain'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM