将 UIBarButtonItems 添加到工具栏时出现问题

发布于 2024-08-25 23:01:38 字数 1032 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅冕 2024-09-01 23:01:38

将此行:

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

纵山崖 2024-09-01 23:01:38

我在文档中发现Apple 有一小段解释 UIToolBar。在本段中,有一个非常小的句子指出:“[..]显示时,此工具栏从活动视图控制器的toolbarItems属性获取其当前的项目集[..]”但是他们没有首先解释该视图必须处于活动状态才能获取这些按钮。因此,这意味着 UIToolBar 已准备好在 viewDidAppear 上检索其按钮,而不是在 viewDidLoad 消息上检索按钮。

- (void)viewDidAppear:(BOOL)animated {
    [[self tableView] reloadData];

    [[self navigationController] setToolbarHidden: NO animated:YES];    
    UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
    [self setToolbarItems:arr animated:YES];

    [super viewDidAppear:animated];
}

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.

- (void)viewDidAppear:(BOOL)animated {
    [[self tableView] reloadData];

    [[self navigationController] setToolbarHidden: NO animated:YES];    
    UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
    [self setToolbarItems:arr animated:YES];

    [super viewDidAppear:animated];
}
触ぅ动初心 2024-09-01 23:01:38

也许你可以使用界面生成器来避免这种情况,但它会更慢

Maybe you can use interface builder to avoid this, however it will be slower

找个人就嫁了吧 2024-09-01 23:01:38

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?

绝對不後悔。 2024-09-01 23:01:38

我制作了一个视图控制器,它是 UITableViewController 的子类,并且通过执行以下操作让工具栏工作:

在 vi​​ewDidLoad 中:

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

NSArray* toolbarItems = [NSArray arrayWithObjects: button1,
                                                   button2,
                                                   button3,
                                                   nil];

[self setToolbarItems:toolbarItems animated:NO];

然后,因为我希望工具栏仅在此屏幕上,所以我将其添加到 viewWillAppear:

[self.navigationController setToolbarHidden:NO animated:YES];

并且最后,我再次在 viewWillDisappear 中隐藏工具栏:

[self.navigationController setToolbarHidden:YES animated:YES];

这对我来说适用于“文本”按钮、内置图标和自定义图标。

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:

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

NSArray* toolbarItems = [NSArray arrayWithObjects: button1,
                                                   button2,
                                                   button3,
                                                   nil];

[self setToolbarItems:toolbarItems animated:NO];

Then, because I want the toolbar only on this screen, I added this to viewWillAppear:

[self.navigationController setToolbarHidden:NO animated:YES];

And finally, I hide the toolbar again in viewWillDisappear:

[self.navigationController setToolbarHidden:YES animated:YES];

This works for me with "text" buttons, built in icons and custom icons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文