Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a bar button item in interface builder?

I am really new to iPhone development and I need help setting up my views.

I have a view that is named FirstViewController.xib and a controller class for this view.

In my MainWindox.xib I have setup a root controller with a moveToNextView function that is connected to the options bar button item.

So when I click on this item the current view switches to the first view and I am able to swticht back. That works fine so far.

The navigation bar at the top of the screen from the MainWindow.xib is displayed in the first view, too. But when I open FirstViewController.xib there isn't any navigation bar defined (but on build&run it is displayed).

This is a problem for me because I want to add a save bar item to the first view. How do I solve that?

like image 324
UpCat Avatar asked Mar 17 '11 08:03

UpCat


People also ask

How do I add a button to my navigation bar?

Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How to add bar button item in Storyboard?

Drop a button on the right-hand side of the navigation bar, then switch to the assistant editor so we can connect it to some code. Ctrl-drag from your new bar button item into your source code, and when you release your mouse button change Connection from “Outlet” to “Action”.


2 Answers

Assuming you have a UIViewController (or UIViewController subclass) that is a child of a UINavigationController. Note, I'm using storyboards so your results may vary when using xibs.

  • If you do not see a UINavigationBar on the interface, try manually changing the simulated metrics.

  • Drag a Navigation Item onto the view (anywhere). You should now have a place to enter the title in the interface builder.

Simulated Metrics

  • Now you can drag Bar Button Items onto the nav bar.

enter image description here

like image 102
anders Avatar answered Oct 25 '22 09:10

anders


You have to do it from code. Add to your FirstViewController class viewDidLoad method:

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(doSave:)];
  self.navigationItem.rightBarButtonItem = anotherButton;
  [anotherButton release];
like image 29
Lukman Avatar answered Oct 25 '22 07:10

Lukman