Search code examples
objective-ccocoa-touchpushviewcontroller

Difficulty with pushViewController pushing two levels at once


I have a list of items in one view controller. I can select an item and a detail view controller is pushed to show details of the selected item. I can then select the edit button (from the nav bar) and an edit view controller is pushed to allow me to edit the details of the item. All of this is working, including cancel and undo.

In the first list view controller, I also have an add button (in the nav bar), where I can add a new record. (This is using core data, but I don't think that has relevance to my question.) I started testing by creating a blank record for the item, and when the detail view controller appears, all of the fields are blank. Then I can select the edit button and edit the details, just as if it were an already populated record. This also works.

What I have been trying to do is select the add button and push the detail view controller and then the edit view controller immediately. The reason I want to do this is so that when I pop the edit view controller, the detail view controller will show. (Then I can pop back to the list view controller or push into the edit view controller again.)

I am having trouble with this.

I tried two pushes in the list view controller, like this:

- (void) add {
    clientInfoTVC.clientMO = [self createNewClient];
    [self.navigationController pushViewController:clientInfoTVC animated:YES]; 
    [self.navigationController pushViewController:clientInfoTVC.clientEditTVC animated:YES]; 
}

where clientInfoTVC.clientEditTVC points to the eidt view controller. This errors SIGABRT and halts when I try an add.

I then tried setting a flag property value in the detail view controller when add is selected, before the first push and then doing a second push in viewWillAppear of the detail view controller. This errors out. I tried moving the second push to the viewDidAppear of the detail view controller, with the same results.

I am lost on this one. Can someone give me some ideas how to push through one view controller to the second one, so that I can maintain the navigation stack and pop back to the middle view controller?


@ljkyser hit on part of the answer. But I also found that simply pushing a view onto the stack does not get it to execute its init, viewWillAppear, and viewDidLoad. I am guessing that since all of this is happening on the main thread, then pushing a view just queues that view controller onto the run loop. Pushing a second view controller removes the first from the run loop and puts the second view controller in the run loop queue. Maybe someone can verify that and describe it more accurately than I just did.

In any case, to get the edit view controller initialized properly, I had to pass a pointer to it, so it can find the managed object it is working with. I had originally instantiated the edit view controller from within the detail view controller. Therefore, in my original code, I was pushing a view controller that was not instantiated yet. To be safe, I instantiated it from the list view controller, and then made sure all of the managed object pointers were set up before I did the double push, like this:

- (void) add {    
    Client *clientNew = [[NSEntityDescription alloc]  init];
    clientNew = [self createNewClient];
    clientInfoTVC.clientMO = clientNew;
    clientEditTVC.clientMO = clientNew;
    [clientNew release];
    [self.navigationController pushViewController:clientInfoTVC animated:NO]; 
    [self.navigationController pushViewController:clientEditTVC animated:YES]; 
}

Confirming the answer given by @ljkyser, I tried these changes while keeping the first animated:YES. With this, the nav bar looked right, but the rest of the view was out of sync. Setting animated:NO fixed that problem.


Solution

  • Try changing your code to this:

    - (void) add {
        clientInfoTVC.clientMO = [self createNewClient];
        [self.navigationController pushViewController:clientInfoTVC animated:NO]; 
        [self.navigationController pushViewController:clientInfoTVC.clientEditTVC animated:YES];
    }
    

    Setting the first pushViewController animated parameter to NO.

    Similar to this SO question: Problem pushing multiple view controllers onto navigation controller stack

    There are other options as well. You can push and pop to specific view controllers if desired (UINavigationController reference) or edit the UINavigationController stack directly (see here).