How to add a bar button item in interface builder?

2020-08-10 07:04发布

问题:

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?

回答1:

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];


回答2:

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.

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



回答3:

Just drag a bar button item onto your nav bar in Interface Builder. Xcode automatically wires it up then you can use it like anything else...