Question

I'm following the CoreDataRecipes app for modaly showing the add screen when I want to add a new item. However I cannot get the bar to display at the top so I can press 'Done' or 'Cancel'.

In the xib calling the modal controller I have the + button linked to modally sliding up the controller via IB.

I have the below in my modal controller

self.navigationItem.title = @"Add";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
    self.navigationController.navigationBarHidden = NO;

In my viewDidLoad

The modal controller displays fine except there is no bar so I cannot leave that screen.

Was it helpful?

Solution

You need to add it before the popover is actually presented.

Where you create the modal popover, you need to create it inside a UINavigationController first.

So, do the following.

 PopoverView *foo = [[PopoverView alloc] initWithNibName:@"PopoverView" bundle:nil];
 // Here you pass through properties if you need too.
 // ...
 UINavigationController *navC = [[UINavigationController alloc] initWithRootView:foo];
 [foo release];

 [self.navigationController presentModalViewController:navC animated:YES];

That will give the modal view the navigation bar which you're trying to edit.

OTHER TIPS

Alternatively, you could maintain your storyboard segue. In Xcode, select the view controller you are trying to transition to and embed it in a navigation controller.

Now in the viewDidLoad of that view controller, add:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];

and lastly the callback:

- (void)cancel {
    [self dismissModalViewControllerAnimated:YES];
}

Or if you just need the look of the bar, not exactly its functionality, you could drag the Navigation Bar (UINavigationBar) or Toolbar (UIToolbar) controll from the Media Library panel onto your view and go from there.

I had a similar predicament whereby I was loading a UITableViewController in a containerView. The containerView was inside a UIViewController which was being presented in a modal fashion.

I, like you, needed the navigation bar to have a title and a Done/Cancel button.

After overflowing the stack, I finally did this -

Dragged a UIView as the first item in the Table View in the IB. This automatically took a height of 44 pts and snapped to the top. It also shifted my first section downwards.

I dragged a UIButton (Done button) inside this view. Created an IBOutlet to it and called

    [self dismissViewControllerAnimated:YES completion:nil];

Disclaimer:

1) This fake nav-bar will scroll along with the tableview. 2) This might not be a solution for what Bot has asked, but it's an option for others who might be looking for something similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top