简体   繁体   中英

iOS: Why isn't this View Controller getting pushed onto the Navigation Controller's stack?

I have a modal view that is a Navigation Controller. When one of the rows in its UITableView gets tapped, the correct View Controller for that row should be initialized and pushed onto the Navigation Controller's stack (so that the screen now shows that View Controller). But it's not working. I've been trying to debug it for a while, and it appears that the Navigation Controller's retain count is 0 at the time pushViewController is called. I assume that means it has been deallocated, and that this is the root of the problem. But I can't figure out why.

In the following code, AddSportDelegate.m presents the modal view that contains the necessary Navigation Controller (_addItemNavController) initialized with the necessary AddItemTableViewController . Tapping on one of the rows of the Table View managed by AddItemViewController calls the showAddItemDataView: method of AddSportDelegate , which in turn should push the correct ViewController onto the _addItemNavController stack. But, as I note in a comment in the code, the retain count of _addItemNavController at that moment is 0.

Note: I realize this code has memory leaks. I deleted some release lines for the sake of brevity. I also haven't included the code for the view controller that is supposed to be getting pushed, since it doesn't have anything at the moment beyond a UILabel identifying that it is the right View Controller.

AddItemDelegate.m

@synthesize addItemNavController = _addItemNavController;

- (void)showAddItemViewController:(UIViewController *)viewController
{
    _parentVC = viewController;
    [_parentVC retain];

    tc = [[AddItemTableViewController alloc] init];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemDone target:self action:@selector(cancelAdd)];

    tc.navigationItem.leftBarButtonItem = cancelButton;
    tc.title = @"Select a Category";

    _addItemNavController = [[AddItemNavController alloc] initWithRootViewController:tc];

    tc.superViewController = _addItemNavController;

    [_parentVC.navigationController presentModalViewController:_addItemNavController animated:YES];
}

- (void)showAddItemDataView:(SportCategory *)category
{               
    [category retain];

    UIViewController *vc;
    if (category.name == @"Soccer") {
        vc = [[AddSoccerDataViewController alloc] init];
    }else{
        vc = [[AddBaseballDataViewController alloc] init];
    }

    //retain count already 0
    NSLog(@"retain count: %i", [_addItemNavController retainCount]);

    [_addItemNavController.navigationController pushViewController:vc animated:YES];
}

AddItemTableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    _addItemDelegate = [[AddItemDelegate alloc] init];

    SportCategory *soccer = [[SportCategory alloc] initWithCategoryName:@"Soccer"];
    SportCategory *baseball = [[SportCategory alloc] initWithCategoryName:@"Baseball"];

    _categories = [[NSArray alloc] initWithObjects:soccer,baseball,nil];

    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SportCategory *selectedCategory = [_categories objectAtIndex:[indexPath row]];
    [_addItemDelegate showAddItemDataView:selectedCategory];
}

I am going to take a shot at this.

if (category.name == @"Soccer")

I come from a java background, but I know a little objective - c. I thought you can't compare strings with == which would mean your view controller was never created. Maybe try a isEqualToString method.

That is my only thought, I could be wrong. But Best of Luck.

The '==' operator isn't the good way to compare strings, but anyway your code should fall into the else part. About your question, _addItemNavController must be nil because your NSLog prints 0 for its retain count. Is the method -(void)showAddItemViewController:(UIViewController *)viewController called somewhere? Your view controller doesn't seem to be initialized.

A bit of sleep helped me find the problem. There were actually two:

1) The final line in AddItemDelegate read:

[_addItemNavController.navigationController pushViewController:vc animated:YES];

However, _addItemNavController IS the navigation controller, so the '.navigationController' part needed to be deleted.

2) I also needed to assign tc.addItemDelegate to self in showAddItemViewController.

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