Adding UIBarButtonItem to UINav..Controller

℡╲_俬逩灬. 提交于 2019-12-18 18:10:11

问题


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:


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.



来源:https://stackoverflow.com/questions/2520912/adding-uibarbuttonitem-to-uinav-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!