将 UIBarButtonItems 添加到工具栏时出现问题
我有一个 UINavigationController 和一个 UITableViewController 。我想在底部显示一个带有 UIBarButtonItem 的工具栏。工具栏已显示,但按钮不会出现。有人知道为什么吗?
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationItem] setTitle:@"Selections List"];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProjectSearch:)] autorelease]];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
[[super tableView] setDataSource: self];
[[super tableView] setDelegate: self];
//Toolbar
UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
[[self navigationController] setToolbarHidden: NO animated:YES];
[[self navigationController] setToolbarItems:arr animated:YES];
}
I have a UINavigationController with a UITableViewController in it. I want to show a ToolBar on the bottom with UIBarButtonItem's. The ToolBar is showing up, but the buttons won't appear. Anyone knows why?
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationItem] setTitle:@"Selections List"];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProjectSearch:)] autorelease]];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
[[super tableView] setDataSource: self];
[[super tableView] setDelegate: self];
//Toolbar
UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
[[self navigationController] setToolbarHidden: NO animated:YES];
[[self navigationController] setToolbarItems:arr animated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将此行:
[[self navigationController] setToolbarItems:arrAnimated:YES];
替换为:
[self setToolbarItems:arranimated:YES];
一般来说,您应该设置
toolbarItems
在您推送的每个单独的视图控制器上,而不是在 UINavigationController 本身上。Replace this line:
[[self navigationController] setToolbarItems:arr animated:YES];
with this:
[self setToolbarItems:arr animated:YES];
In general, you should set
toolbarItems
on each individual view controller that you push, and not on your UINavigationController itself.我在文档中发现Apple 有一小段解释 UIToolBar。在本段中,有一个非常小的句子指出:“[..]显示时,此工具栏从活动视图控制器的toolbarItems属性获取其当前的项目集[..]”但是他们没有首先解释该视图必须处于活动状态才能获取这些按钮。因此,这意味着 UIToolBar 已准备好在 viewDidAppear 上检索其按钮,而不是在 viewDidLoad 消息上检索按钮。
I found out in the documentation of Apple there is small paragraph explaining the UIToolBar. In this paragraph there is a very tiny sentence stating: "[..] When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller [..]" But they don't explain that view first has to be active to obtain these buttons. So that means that the UIToolBar is ready to retrieve it's Buttons on viewDidAppear and NOT on viewDidLoad message.
也许你可以使用界面生成器来避免这种情况,但它会更慢
Maybe you can use interface builder to avoid this, however it will be slower
http://developer.apple.com/ iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
“导航控制器对象现在在其视图层次结构中管理一个可选工具栏。显示时,此工具栏从toolbarItems获取其当前的项目集活动视图控制器的属性。”
您是否尝试过为 tableview 子类化 UITableViewController 并使用适当的toolbarItems 属性进行设置?
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
"The navigation controller object now manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller."
Have you tried subclassing UITableViewController for your tableview and setting up with the appropriate toolbarItems property?
我制作了一个视图控制器,它是 UITableViewController 的子类,并且通过执行以下操作让工具栏工作:
在 viewDidLoad 中:
然后,因为我希望工具栏仅在此屏幕上,所以我将其添加到 viewWillAppear:
并且最后,我再次在 viewWillDisappear 中隐藏工具栏:
这对我来说适用于“文本”按钮、内置图标和自定义图标。
I've made a view controller, which is a subclass of UITableViewController, and I've got the toolbar working by doing the following:
In viewDidLoad:
Then, because I want the toolbar only on this screen, I added this to viewWillAppear:
And finally, I hide the toolbar again in viewWillDisappear:
This works for me with "text" buttons, built in icons and custom icons.