【问题标题】:UIBarButtonItem not showing up in UINavigationControllerUIBarButtonItem 未显示在 UINavigationController 中
【发布时间】:2012-04-29 20:01:05
【问题描述】:

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

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...)

【问题讨论】:

  • 按下 ViewController 后设置第二个按钮或在特定 itemsByTitleVC 控制器中设置第二个按钮
  • 都没有用。我之前尝试在正在推送的 viewController 的 viewDidLoad 中添加 rightBarButtonItem - 没有用...

标签: iphone uinavigationcontroller uinavigationitem rightbarbuttonitem


【解决方案1】:

您将 rightBarButtonItem 设置为 filterButton,但不应该是 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;

【讨论】:

  • 抱歉 :-) 这确实是我的错字 - 但仅在这篇文章中 - 我在代码中正确输入了它 - 但它不起作用......
  • 但是你有没有试过在viewDidLoad方法的itemsByTitleVC Controller中创建右栏按钮呢?看起来您设置了 rightBarButton 但它确实在新视图中被覆盖。左键可能正在工作,因为它是始终显示的标准后退按钮..
  • 我确实尝试在“itemsByTitleVC”视图控制器的 viewDidLoad 方法中设置搜索按钮 - 它仍然不起作用。这是我使用的代码 - 完全相同的代码 - 但也许它需要调整,因为它现在在 viewDidLoad 中?:UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(goSearching:)]; self.navigationItem.rightBarButtonItem = searchButton;
  • 我刚刚查看了我自己的项目,我正在做同样的事情。这是我正在使用的代码。 // 创建左侧扫描按钮 UIBarButtonItem *scanButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(scanButtonClicked:)]; [self.navigationItem setLeftBarButtonItem:scanButton];我也在新推送的 ViewController 的 viewDidLoad 方法中执行此操作。
  • 我看到你正在使用[self.navigationItem setLeftBarButtonItem:scanButton]; - 换句话说,你正在使用一种方法,而我一直在使用点语法self.navigationItem.rightBarButtonItem = searchButton; 我想知道这是否是问题......让见...
【解决方案2】:

也许你把 searchButton 和 filterButton 混在一起了?

编辑:

我再次查看了您的代码,发现您在 :didSelectRowAtIndexPath 中执行此操作。

当你设置self.navigationItem.rightBarButtonItem时,实际上你改变的是当前navigationController的按钮,而不是itemsByTitleVC的按钮。

所以解决办法是,把这段代码移到 itemsByTitleVC 里面的某个地方,比如 viewDidLoad: 或 viewWillAppear:。

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

【讨论】:

  • 再次,很好 - 但这只是这篇文章中的一个错字 - 我已经更正了(见上文) - 我一直在代码中使用“searchButton”,所以不是问题...
【解决方案3】:

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

self.navigationItem.rightBarButtonItem 

是 UIView 类的只读属性。

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 在这里使用设置器没有区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2012-01-15
    • 2014-08-06
    相关资源
    最近更新 更多