Search code examples
objective-ciosdelegatesuisplitviewcontroller

UIBarButtonItem disappears when replacing DetailViewController, reappears after rotating


After reading multiple threads relating to this I can only conclude that I must be setting my delegate at the wrong time, but I can't figure out where to assign the delegate to fix this.

I replace the detail view controller upon selection of a table row in the master like so:

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
CustomerDetailViewController *newvc = [[CustomerDetailViewController alloc] initWithNibName:@"CustomerDetailViewController" bundle:nil withCustomer:[custData objectForKey:@"name"]];            
[app.detailNavigationController setViewControllers:[NSArray arrayWithObjects:newvc, nil]];

This sets the view controller. In CustomerDetailViewController, I assign the delegate like so:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        app.splitViewController.delegate = self;
    }
    return self;
}

What happens is if a table row is selected in portrait mode, I do not get the barbuttonitem appearing. However, if I rotate to landscape and then back to portrait, the barbuttonitem does appear.

I figured setting my delegate in initWithNibName would fix this but apparently not.

Can anyone tell me where/when to assign the delegate??


Solution

  • I believe I have found the solution - it appears to be a duplicate of this question.