ios - UIBarButton 不会显示在我的 UINavigationController 中

标签 ios uinavigationcontroller uinavigationbar uibarbuttonitem

我想制作一个带有导航栏和表格 View 的简单 View 。所以我创建了 UINavigationController 的子类,并在其中添加了一个 UITableView。我的也是 UITableViewDataSourceUITableViewDelegate。现在我想在导航栏中添加 UIBarButton 但它不起作用:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:[[Translator instance] getTranslation:@"Back"] 
                                                                  style:UIBarButtonItemStylePlain 
                                                                 target:self.parentViewController  
                                                                 action:@selector(dismissFiltersModal:)];          
self.navigationItem.backBarButtonItem = anotherButton;

显示 View 但不显示按钮没有错误。我该怎么做才能显示它?

最佳答案

贾法尔,

正如 Apple 文档所建议的那样,您应该避免子类化 UINavigationController

This class is not intended for subclassing.

说到这里,您可以按照这些简单的步骤(给您的一些建议):

  • 创建扩展UIViewController 的 Controller (例如MyController)并在此处添加表格。将您的 Controller 设置为该表的委托(delegate)和数据源。如果您是由 xib 创建的,则需要为您的 TableView 提供导出。

[附言。你也可以子类化 UITableViewController,它有自己的 TableView ]

  • MyControllerviewDidLoad中自定义导航栏。

例如

UIBarButtonItem* myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(someSelector)];
self.navigationItem.leftBarButtonItem = myButton;

您还可以自定义标题。

例如

self.title = @"Your custom title";
  • 在代码的某处创建一个 UINavigationController 并将 MyController 的实例添加为 Root View Controller 。

例如:

MyController* myCtr = // alloc-init here
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:myCtr];

关于ios - UIBarButton 不会显示在我的 UINavigationController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11159707/

相关文章:

ios - 某些 UIViewController 中的 XAMARIN.IOS UITabBarController

TableView 导航栏上的 IOS 按钮不起作用

ios - UINavigationBar下如何添加灰色的描述栏?

ios - Swift Eureka 表单 : how do you limit the number of rows in a multivalued section?

ios - 从文本字段/ TextView 中获取文本

ios - 如何在 Swift 中调用 `setViewControllers: animated:`?

ios7 - 如何在 iOS 7 中专门为 cocos2D 隐藏导航栏?

ios - 如何使用 Buck 链接预建库?

ios - iOS 8 中没有出现 UITableViewCell 约束。我该如何缓解这种情况?

swift - 如何摆脱导航 Controller 并转到它之前的 Controller ?