How to add bar button to uinavigationbar in iphone

柔情痞子 提交于 2019-12-12 05:39:42

问题


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;       

but its not working... should i add the navigationbar also programmatically or should my class a uinavigationcontroller

Thank u


回答1:


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

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



来源:https://stackoverflow.com/questions/4216259/how-to-add-bar-button-to-uinavigationbar-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!