2
votes

i am not sure what i am missing here. I Have a custom UINavigationController and i am trying to add a persistant UIBarButtonItem to the bar.

-(void)viewDidLoad
{
    self.navigationBar.barStyle = UIBarStyleBlack;
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Nope..."
               style:UIBarButtonItemStyleBordered
                 target:self
                 action:@selector(goBack:)]; 
    self.navigationItem.leftBarButtonItem =bbi;
    [bbi release]; 
}
-(void)goBack:(id)sender
{
    NSLog(@"go back now");
}

what am i missing here? - BTW, i do not want to/ will not use IB.

UPDATE: Currently this is the closest i can get:

-(void)viewDidLoad
{
    self.navigationBar.barStyle = UIBarStyleBlack;
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
    navBar.barStyle = UIBarStyleBlack;
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Currently Playing..."];
    [navBar pushNavigationItem:navItem animated:NO];
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
    navItem.rightBarButtonItem = editButton;

    [self.view addSubview:navBar];

    [editButton release];
    [navItem release];
    [navBar release]; 
    [super viewDidLoad];
}

It's terrible i have to add an entire navbar to a UINavigationController that already has a navbar....if i try to use the existing, i get this error:

'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'

....really???

1
so what happens? Do you see your UINavigationController but it does not have the custom bar button? Did you put break in the above method and it does get called? You stepped through it and non of the object references are nil? You stepped through method that calls this method and you can see that the navigation bar is properly installed?stefanB
The debugger shows that it is indeed there, but the actual display has nothing. For what it's worth, the bar is black, as declared.mlecho
Put your top code in your root view controller, not in your UINavigationController. Also, the reason for your error in the second case is that you don't push nav bars like that. When you are using navigation controllers, you push new view controllers onto the navigation controllers stack. These new view controllers then customize their navigation items as necessary.Jason Coco
so if i wanted to reuse this custom UINavigationController, every time i create it, i have to reset the navbar and that button with the root controller? That sounds crazy busy. I'd prefere that i don't have to remember each time, and have it automatic as part of the unique nature of that custom class. There is no way to put this logic into the class?mlecho
Usually you don't subclass the UINavigationController. Instead you create your own subclass of UIViewController and use it as the root view controller when you create the UINavigationController. All the customization then goes in your custom UIViewController.Jason Coco

1 Answers

22
votes

navigationItem must not be set on the UINavigationController instance but on the view controller of the view which is displayed "inside" the navigation controller.

Setting self.navigationItem on your navigation controller would work if your controller was itself pushed into another navigation controller.