我想制作一个带有导航栏和表格 View 的简单 View 。所以我创建了 UINavigationController
的子类,并在其中添加了一个 UITableView
。我的也是 UITableViewDataSource
和 UITableViewDelegate
。现在我想在导航栏中添加 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 ]
- 在
MyController
的viewDidLoad
中自定义导航栏。
例如
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/