我有一个UINavigationController,我的AHViewController作为根视图控制器。

然后,我尝试从AHViewController的viewDidLoad中向其添加一个左键按钮项:

UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"grid.png"] forState:UIControlStateNormal];
    UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    //[self.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [backButtonBarItem release];


但是,我什么也没看到。为什么是这样?

最佳答案

代替:

[self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];


做这个:

[self.navigationItem setLeftBarButtonItem:backButtonBarItem];


UINavigationController使用顶视图控制器的navigationItem,而不是其(UINavigationController的)导航项属性。

关于iphone - 向左导航栏添加leftBarButtonItem的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10391299/

10-12 16:10