Create a UIView that can be presented modally OR pushed onto the navigation stack

85

In order to accomplish this I:

Removed the nav bar from my view. When launching modally, first created a nav controller, and then displayed the nav controller modally with my view as the root view (even though I didn't plan on pushing anything else onto the stack). So changed this:

StoreDetailView *storeDetailView = [[StoreDetailView alloc] initWithNibName:@"StoreDetailView" bundle:nil];
// ... configure the view, including setting delegate...
[self presentViewController:storeDetailView animated:YES completion: nil];

to this:

StoreDetailView *storeDetailView = [[StoreDetailView alloc] initWithNibName:@"StoreDetailView" bundle:nil];
// ... configure the view, including setting delegate...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:storeDetailView];
[self presentViewController:navController animated:YES completion: nil];

And then in the StoreDetailView, determined what the nav bar should look like based on whether the delegate was set:

if (self.delegate == nil) {
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
} else {
    [self setEditing:TRUE];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
}
Share:
85

Related videos on Youtube

Sasha
Author by

Sasha

Updated on November 22, 2022

Comments

  • Sasha
    Sasha 12 months

    I have an item detail view which I would like to use for two purposes:

    1) to create a new item 2) to edit an existing item

    When editing, the view will be pushed onto the navigation stack, getting the nav bar from it's parent.

    On item creation, I want to present the view modally, but still have a navigation bar at the top, with "Done" and "Cancel" buttons.

    What I don't want is to ever see the view with two nav bars, or none.

    How would I implement this?