[英]How to add a NavigationBarItem and UIBarButtonItem programmatically?
I presented a View using a call to presentModalViewController
. 我使用对
presentModalViewController
的调用提出了一个View。
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
. 我的新View没有导航栏,因此我从库中添加了一个导航栏,但是现在当我在控制器的
viewDidLoad
中创建UIBarButtonItem
并将其rightBarButtonItem
为navigationBarItem
rightBarButtonItem
时。
But when I run my app, the Navigation bar is there but there is no button. 但是,当我运行我的应用程序时,导航栏在那里,但是没有按钮。
Did I miss a step or something ? 我错过了一步吗?
Actually it is easier than this: 其实比这容易:
Drag a UINavigationBar into your app. 将UINavigationBar拖到您的应用程序中。 Note that you will also get a UINavigationItem when you do this... make sure you keep it.
请注意,执行此操作时还将获得UINavigationItem。请确保将其保留。
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. Xcode可能会指示错误(您正在覆盖UIViewController的内置属性),因此必须手动合成getter和setter。 Add the following line below the @implementation statement for the class:
在该类的@implementation语句下面添加以下行:
@synthesize navigationItem;
In Interface Builder, drag a connection from the new navigationItem property to your custom navigation item. 在Interface Builder中,将连接从新的navigationItem属性拖动到您的自定义导航项。
You can now access your navigation item using self.navigationItem, just like you would normally. 现在,您可以像通常那样使用self.navigationItem访问导航项。
Just adding a navigation bar manually will not make the self.navigationController
property actual and it's value is nil
, which, I believe, you're using for adding a UIBarButtonItem
instance. 仅手动添加导航栏不会使
self.navigationController
属性成为实际属性,其值为nil
,我相信您正在使用它来添加UIBarButtonItem
实例。
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 in viewDidLoad
use something like: 我建议在控制器中声明一个
UINavigationBar
IBOutlet,在NIB编辑器中将其与您添加的导航栏连接,然后在viewDidLoad
使用类似以下内容:
@interface ...
@property (nonatomic, retain) IBOutlet UINavigationBar *theBar;
@end
@implementation ...
@synthesize theBar;
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *yourButton = ...;
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"MyTitle"];
item.rightBarButtonItem = yourButton;
[theBar pushNavigationItem: item animated:NO];
[item release];
...
}
@end
I thought navigationBarItem property of UIViewController is work only if your ViewController is present with UINavigationController 我认为只有当您的ViewController与UINavigationController存在时,UIViewController的navigationBarItem属性才有效
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 如果使用界面构建器添加导航栏或使用代码添加导航栏,则需要使用-(void)pushNavigationItem:(UINavigationItem *)item animationed:(BOOL)animated
to add UINavigationItem yourself 自己添加UINavigationItem
you can create a UINavigationController use - (id)initWithRootViewController:(UIViewController *)yourViewController 您可以使用以下方法创建UINavigationController-(id)initWithRootViewController:(UIViewController *)yourViewController
and use presentModalViewController to show UINavigationController that you create 并使用presentModalViewController来显示您创建的UINavigationController
then you can use navigationBarItem property of UIViewController to set rightBarButtonItem in viewDidLoad 那么您可以使用UIViewController的navigationBarItem属性在viewDidLoad中设置rightBarButtonItem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.