如何在 iphone 中向 uinavigationbar 添加条形按钮 [英] How to add bar button to uinavigationbar in iphone
问题描述
我的班级是一个普通的视图控制器,我通过 IB 添加了一个导航栏,我正在使用以下代码以编程方式将左侧的栏按钮项添加到导航栏
My class is a normal view controller and I added a navigation bar through IB and i am adding a barbutton item on left to the navigation bar programmatically using the following code
UIBarButtonItem *addButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(tourBtnClicked:)];
self.navigationItem.leftBarButtonItem = addButton1;
但它不起作用......我应该以编程方式添加导航栏还是我的班级应该是一个 uinavigationcontroller
but its not working... should i add the navigationbar also programmatically or should my class a uinavigationcontroller
谢谢你
推荐答案
如果您没有使用 UINavigationController
,您将需要添加代码来以编程方式管理导航栏.例如,您需要使用以下方法向导航栏发送消息以将导航项推入其堆栈:
If you're not using a UINavigationController
, you will need to add code to manage the navigation bar programmatically. For example, you'd need to send a message to the navigation bar to push a navigation item onto its stack using the following method:
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
当您尝试设置其属性时,您还应该确保 navigationItem
属性当前不是 nil
.来自 navigationItem
属性的 UIViewController
类参考:
You should also make sure the the navigationItem
property isn't currently nil
when you attempt to set its properties. From the UIViewController
class reference for the navigationItem
property:
确保导航项是已配置,您可以覆盖它属性并添加代码以加载栏按钮项目在那里或加载项目在您的视图控制器中初始化代码
To ensure the navigation item is configured, you can override this property and add code to load the bar button items there or load the items in your view controller’s initialization code
这篇关于如何在 iphone 中向 uinavigationbar 添加条形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!