0

我正在尝试将 2 个按钮添加到 UINavigationController 的导航栏:

1)左侧的标准“后退”按钮 - 有效,并且

2)右侧的“搜索”按钮 - 不显示。

这是代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// 1st button - this shows up correctly:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"MAIN";
self.navigationItem.backBarButtonItem = backButton;    

// 2nd. button - this one does not show up:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                           target:self
                                           action:@selector(goSearching:)];
self.navigationItem.rightBarButtonItem = searchButton;


itemsByTitleVC *itemsView = [[itemsByTitleVC alloc] initWithNibName:@"itemsByTitleVC" bundle:nil];


[self.navigationController pushViewController:itemsView animated:YES];

}

有人知道为什么这不起作用吗?(对于它的价值,我使用的是 Xcode 4.2,带有 Storyboard ......)

4

4 回答 4

5

Your setting the rightBarButtonItem to filterButton, but shouldn't it be searchButton?

// 2nd. button - this one does not show up:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                           target:self
                                           action:@selector(goSearching:)];
// Here I think you wanna add the searchButton and not the filterButton..
self.navigationItem.rightBarButtonItem = searchButton;
于 2012-04-18T12:02:49.467 回答
0

Maybe you mixed searchButton up with filterButton?

EDIT:

Once again I looked at your code and I noticed that you are doing this in :didSelectRowAtIndexPath.

When you set self.navigationItem.rightBarButtonItem, actually your are changing the button of current navigationController, not the button of itemsByTitleVC.

So the resolution is, move this piece of code into somethere inside itemsByTitleVC, like viewDidLoad: or viewWillAppear:.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

于 2012-04-18T12:03:51.480 回答
0

我今天遇到了这个问题,最终我发现我的(和你的)

self.navigationItem.rightBarButtonItem 

是 UIView 类的只读属性。

声明您自己的指向 的属性,barbuttonItem您可以修改该按钮。

于 2012-09-03T07:58:36.350 回答
-1

我也遇到过这个问题,最后通过setter方法设置rightBarButtonItem解决了。喜欢[self.navigationItem setRightBarButtonItem:saveButton animated:YES];

于 2016-07-20T07:51:17.807 回答