如何在 iPhone 的导航栏中添加右栏按钮
我想在导航栏中添加一个右栏按钮项目,以便单击时执行某些功能。
我创建了以下代码来添加右侧栏按钮项目,但完成后,栏按钮项目未显示在导航栏中:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.
I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
假设您有一个 UIViewController,如下所示:
并将其添加到导航控制器:
这样 rightBarButtonItem 将不会显示:
但这样它将显示:
使用您自己的视图控制器而不是导航控制器来引用导航项。
Let assume, you have a UIViewController, like this:
And you added it to navigation controller:
In this way rightBarButtonItem will NOT getting displayed:
BUT this way IT WILL appear:
Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.
尝试其他答案。我发布了这个答案,这样如果您的视图控制器没有导航控制器(我认为这是问题所在),它就可以工作。
Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.
在viewdidload中添加这段代码
Add this code in viewdidload
这里的大多数答案都解决了在显示 UINavigationBar 的 rightBarButtonItem 后在新 VC 中设置它的问题。我的需求(实际上由 János 回答)是从调用 UIViewController 中设置 UINavigationItem 项。因此,有以下代码(谢谢,János)。
// 来自自身(又名调用控制器):
现在,理所当然,这对于 János 的回答来说似乎是多余的,但我需要更多的代表点才能标记他的回答。 ;-)
Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).
// from within self (aka the calling controller):
Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)
我想这将是更完整的回应,并且在任何情况下都应该有所帮助:
I guess it will be more complete response and it should help in any situation:
使用以下代码:
希望这可以帮助您。
享受!
Use the following code:
Hope this helps you out.
Enjoy!
迅速:
Swift:
进而:
and then: