IOS - how to create a navigation bar item in a controller?

I am trying to create a back button on a navigation bar.

Here is my code: -

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease]; self.navigationItem.rightBarButtonItem = barButton; 

But nothing is displayed on the navigation bar.

I am using a UIViewController, not a UINavigationController.

Is the UINavigationController method the only way to achieve this?

Any help would be really appreciated.

Any reference to a good tutorial would be great.

Thanks.

+4
source share
6 answers

Without a viewcontroller with a navigation controller (i.e. viewController.navigationController! = Nil) you cannot add it this way.

One thing you can do is that it is created using nib - just drag the panel item to the navigation bar and link it using IBAction.

+5
source

I would recommend getting this view controller out of the nvigation controller - you will get all these things for free:

 UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil]; UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc]; 
+5
source

If you have a view controller created from an NIB file in Interface Builder, and there is a navigation bar on the design surface of the View controller, the easiest way to do this is to drag the panel button element from the object inspector object directly to the right side of the navigation panel. You will then create an IBAction in your header and implementation to which you can connect.

+3
source

I achieve this by inserting a button directly into the UINavigationBar:

 [yourUINavBar insertSubview:yourButton atIndex:1]; 
+1
source

The UIViewController -navigationItem method works only with the UINavigationController or other protected UIViewController. You will need to access the UINavigationBar and set the item directly.

0
source

If you want to have a navigation bar and work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use this UINavigationController, where you are currently using the UIViewController.

Learn more about the UINavigationController at developer.apple.com.

0
source

All Articles