简体   繁体   English

如何以编程方式添加NavigationBarItem和UIBarButtonItem?

[英]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并将其rightBarButtonItemnavigationBarItem 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: 其实比这容易:

  1. 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。请确保将其保留。

  2. Add the following property to your app: 将以下属性添加到您的应用程序:

    @property (weak, nonatomic) IBOutlet UINavigationItem *navigationItem;

  3. 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;

  4. 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.

相关问题 如何以编程方式添加UIBarButtonItem? - How to add a UIBarButtonItem programmatically? 如何以编程方式向UIBarButtonItem添加填充 - How to add padding to UIBarButtonItem programmatically 在Swift中以编程方式添加UIBarButtonItem操作 - Add UIBarButtonItem action in Swift programmatically 在iOS上以编程方式添加UIBarButtonItem操作 - Add UIBarButtonItem action programmatically on iOS 如何在不使用图像的情况下以编程方式将自定义宽度的UIBarButtonItem添加到UINavigationItem - How to add a UIBarButtonItem of custom width to a UINavigationItem programmatically without using an image 如何以编程方式将UIBarButtonItem添加到拖到视图上的UINavigationBar - How to add a UIBarButtonItem programmatically to UINavigationBar dragged onto view 如何以编程方式将UIBarButtonItem添加到导航控制器? - How can I add UIBarButtonItem to my navigation controller programmatically? 如何有条件地向将在 NavigationView 中显示的 SwiftUI View 添加尾随的 navigationbaritem? - How to add a trailing navigationbaritem conditionally to a SwiftUI View which will be presented in a NavigationView? 如何以编程方式禁用/启用UIBarButtonItem - How to programmatically disable/enable a UIBarButtonItem 如何将边框添加到UIBarButtonItem - How to add the borders to the UIBarButtonItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM