3

My question is next: In interface builder i create UINavigationBar and I want to create 'Back' button item, but I dont see any button.

I use this code:

    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
    myBarButtonItem.title = @"Back";
    mynavBar.backItem.backBarButtonItem = myBarButtonItem;

mynavBar - this is my IBOutlet.

Thanks for help!

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

3 Answers3

5

You can use a navigation bar as a standalone control or in conjunction with a navigation controller. When you use a navigation bar as a standalone control you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.

So in your case you would use something like this:

UIBarButtonItem *myBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
myBarButtonItem.title = @"Back";

UINavigationItem *right = [[[UINavigationItem alloc] initWithTitle:@"Hello!"] autorelease];
right.leftBarButtonItem = myBarButtonItem;

[mynavBar pushNavigationItem:right animated:YES];

You may want to look into using UINavigationViewController though.

ihuk
  • 1,336
  • 9
  • 15
  • for right item i use next code - right.backBarButtonItem = myBarButtonItem. and this work. please change this, and I vote up you comment. thanks for response! – Matrosov Oleksandr Mar 09 '12 at 01:44
  • Not sure how your code looks like but simply changing `right.leftBarButtonItem` to `right.backBarButtonItem` in the example above would not have the desired effect. UINavigationItem's backBarButtonItem property is used when item is immediately below the top item in the stack. So in the example above you could add `mynavBar.topItem.backBarButtonItem = myBarButtonItem;` instead of `right.leftBarButtonItem = myBarButtonItem` and then push `right` onto the navigation stack. – ihuk Mar 09 '12 at 09:22
2

If you want a custom button on the left, use mynavBar.leftBarButtonItem instead of backItem. The backItem will only be visible, after you presented another viewcontroller via pushViewController:. (If you didn't set you own backbutton, the default backButton with the title of the previous viewController will be created automatically.)

//edit: perhaps you look for that: Draw custom Back button on iPhone Navigation Bar

Community
  • 1
  • 1
calimarkus
  • 9,955
  • 2
  • 28
  • 48
  • thanks i uses this - mynavBar.topItem.leftBarButtonItem = myBarButtonItem; and it work. but if we use navigtionbarviewcontroller back button look for another (not a round rect). in this case this work but button is round rect, not a back button. – Matrosov Oleksandr Mar 08 '12 at 13:15
  • If you just want to replace the text on the original back button, you have to add a backButton like you did in your example, but it will first appear, when you push another viewController. (because the current controller backButtonItem represents the currentViewController, when another viewcontroller is on top of that) – calimarkus Mar 08 '12 at 13:21
  • You cannot create a backbutton-formed button on your own by using system calls. Perhaps this helps you: http://stackoverflow.com/questions/4260238/draw-custom-back-button-on-iphone-navigation-bar – calimarkus Mar 08 '12 at 13:23
0

I think this is the actual way apple want this to be implemented.

Put UINavigationBar

Set outlet to the UINavigationItem

This is the catch

Override navigationItem property to return the UINavigationItem you created.

That's it.

-(UINavigationItem *) navigationItem
{
    return self.navigationItem1;
}

If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

user4234
  • 1,523
  • 1
  • 20
  • 37