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

发布于 2024-12-21 07:26:02 字数 271 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨曦÷微暖 2024-12-28 07:26:02

仅手动添加导航栏不会使 self.navigationController 属性成为实际的,它的值为 nil,我相信,您正在使用它来添加 UIBarButtonItem 实例。

我建议在控制器中声明一个 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

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.

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:

@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
忆伤 2024-12-28 07:26:02

我以为
仅当您的 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

瑾夏年华 2024-12-28 07:26:02

实际上比这更容易:

  1. 将 UINavigationBar 拖到您的应用程序中。请注意,执行此操作时您还将获得一个 UINavigationItem...请确保保留它。

  2. 将以下属性添加到您的应用中:

    @property(弱,非原子)IBOutlet UINavigationItem *navigationItem;

  3. Xcode 可能会指示错误(您正在覆盖 UIViewController 的内置属性),因此您必须手动合成 getter 和二传手。在该类的 @implementation 语句下方添加以下行:

    @synthesize navigationItem;

  4. ,将连接从新的 navigationItem 属性拖动到自定义导航项。

您现在可以使用 self.navigationItem 访问导航项,就像平常一样。

Actually it is easier than this:

  1. Drag a UINavigationBar into your app. Note that you will also get a UINavigationItem when you do this... make sure you keep it.

  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. Add the following line below the @implementation statement for the class:

    @synthesize navigationItem;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文