如何以编程方式添加 NavigationBarItem 和 UIBarButtonItem?
我使用 presentModalViewController
调用来呈现一个视图。
我的新视图没有导航栏,因此我从库中添加了一个导航栏,但现在当我在控制器的 viewDidLoad 中创建一个 UIBarButtonItem 并将其设置为 < navigationBarItem
的 code>rightBarButtonItem。
但是当我运行我的应用程序时,导航栏就在那里,但没有按钮。
我是否错过了一步或什么?
I presented a View using a call to presentModalViewController
.
My new View didn't have a navigation bar, so I added one from the Library but now when I created a UIBarButtonItem
in viewDidLoad
of my controller, and set it to rightBarButtonItem
of the navigationBarItem
.
But when I run my app, the Navigation bar is there but there is no button.
Did I miss a step or something ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅手动添加导航栏不会使
self.navigationController
属性成为实际的,它的值为nil
,我相信,您正在使用它来添加UIBarButtonItem
实例。我建议在控制器中声明一个
UINavigationBar
IBOutlet,在 NIB 编辑器中将其与您添加的导航栏连接起来,然后在viewDidLoad
中使用类似以下内容的内容:Just adding a navigation bar manually will not make the
self.navigationController
property actual and it's value isnil
, which, I believe, you're using for adding aUIBarButtonItem
instance.What I'd recommend is to declare a
UINavigationBar
IBOutlet in your controller, connect it in NIB editor with navigation bar you've added and after that inviewDidLoad
use something like:我以为
仅当您的 ViewController 与 UINavigationController 一起存在
并且您使用界面生成器添加导航栏时, UIViewController 的 navigationBarItem 属性才起作用
或者用代码添加它,然后你需要使用
- (void)pushNavigationItem:(UINavigationItem *)item 动画:(BOOL)animated
自己添加 UINavigationItem
你可以创建一个 UINavigationController
使用 - (id)initWithRootViewController:(UIViewController *)yourViewController
并使用presentModalViewController来显示您创建的UINavigationController,
然后您可以使用UIViewController的navigationBarItem属性在viewDidLoad中设置rightBarButtonItem
I thought
navigationBarItem property of UIViewController is work only if your ViewController is present with UINavigationController
if you add a navigation bar with interface builder
or add it with code ,then you need to use
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
to add UINavigationItem yourself
you can create a UINavigationController
use - (id)initWithRootViewController:(UIViewController *)yourViewController
and use presentModalViewController to show UINavigationController that you create
then you can use navigationBarItem property of UIViewController to set rightBarButtonItem in viewDidLoad
实际上比这更容易:
将 UINavigationBar 拖到您的应用程序中。请注意,执行此操作时您还将获得一个 UINavigationItem...请确保保留它。
将以下属性添加到您的应用中:
@property(弱,非原子)IBOutlet UINavigationItem *navigationItem;
Xcode 可能会指示错误(您正在覆盖 UIViewController 的内置属性),因此您必须手动合成 getter 和二传手。在该类的 @implementation 语句下方添加以下行:
@synthesize navigationItem;
您现在可以使用 self.navigationItem 访问导航项,就像平常一样。
Actually it is easier than this:
Drag a UINavigationBar into your app. Note that you will also get a UINavigationItem when you do this... make sure you keep it.
Add the following property to your app:
@property (weak, nonatomic) IBOutlet UINavigationItem *navigationItem;
Xcode will likely indicate an error (you are overriding a built in property of UIViewController), so you have to manually synthesize the getter and setter. Add the following line below the @implementation statement for the class:
@synthesize navigationItem;
In Interface Builder, drag a connection from the new navigationItem property to your custom navigation item.
You can now access your navigation item using self.navigationItem, just like you would normally.