本文介绍了将项目添加到NavigationBar(不使用UINavigationController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController与其UITableView,并且还添加了一个UINavigationBar。
如何在程序中添加和编辑按钮和+按钮?
(我已经尝试使用IB,但标题总是替换,而不是其他项目添加)
我没有使用UINavigationController。




$ b

这是我试过的没有成功:

c $ c> UIBarButtonItem * barButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:nil
action:nil];
UINavigationItem * editItem = [[UINavigationItem alloc] initWithTitle:@Title];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];


解决方案

您的 UIViewController 有一个 navigationItem 属性。您可以使用 self.navigationItem.leftBarButtonItem = ... self.navigationItem.rightBarButtonItem = ...设置左侧和右侧栏按钮项。 / code>



编辑



请参考您的 UINavigationBar
然后我想你会添加一个 UINavigationItem

  UINavigationItem * item = [[UINavigationItem assign] initWithTitle:@A Title]; 
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; //或将其保存为实例变量

,然后设置该项目的左右按钮: p>

  theNavigationBar.topItem.leftBarButtonItem = ...; 
theNavigationBar.topItem.rightBarButtonItem = ...;

我没有尝试过,但我认为它应该工作。


I have a UIViewController with a UITableView in it, and also added a UINavigationBar.How can I add and "edit" button and a "+" button in that bar programmatically?(I have tried using IB, but the title is always replaced, and not other items are added)I am not using a UINavigationController. is my UIViewController standing alone.

This is what I have tried without success:

UIBarButtonItem *barButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
                                                  target:nil
                                                  action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];
解决方案

Your UIViewController has a navigationItem property. You can set the left and right bar button items with self.navigationItem.leftBarButtonItem = ... and self.navigationItem.rightBarButtonItem = ...

Edit:

OK, I assume you have a reference to your UINavigationBar?Then I guess you'd add a single UINavigationItem to it:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable

and then set that item's left and right buttons:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

I haven't tried this, but I think it should work.

这篇关于将项目添加到NavigationBar(不使用UINavigationController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:07