iOS - 如何在视图控制器中创建导航栏项目?

发布于 2024-12-02 08:46:58 字数 454 浏览 4 评论 0原文

我一直在尝试创建一个导航栏后退按钮。

这是我的代码:-

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;

但它没有在导航栏上显示任何内容。

我使用的是 UIViewController,而不是 UINavigationController。

UINavigationController 是实现这一目标的唯一方法吗?

任何帮助将不胜感激。

任何优秀教程的链接都会很棒。

谢谢。

I have been trying to create a navigation bar back button.

Here is my code :-

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;

But it isn't displaying anything on navigation bar.

I am using UIViewController, not an UINavigationController.

Is UINavigationController is the only way to achieve this?

Any help would be really appreciated.

Any link to a good tutorial will be great.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

流星番茄 2024-12-09 08:46:58

如果视图控制器没有导航控制器(即 viewController.navigationController != nil),则无法以这种方式添加它。

您可以做的一件事是,如果它是由 nib 创建的,只需将一个栏按钮项目拖到导航栏中并通过 IBAction 链接它。

Without the viewcontroller having a navigation controller (i.e viewController.navigationController != nil) you cannot add it in this manner.

One thing you can do is if it is being created by the nib is to just drag a bar button item into a navigation bar and link it via IBAction.

碍人泪离人颜 2024-12-09 08:46:58

我建议将此视图控制器从导航控制器中推出 - 您将免费获得所有这些东西:

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];

I'd recommend pushing this view controller out of a nvigationcontroller - you will get all those things for free:

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];
时光沙漏 2024-12-09 08:46:58

如果您的视图控制器是从 Interface Builder 中的 NIB 文件创建的,并且视图控制器的设计表面上有一个导航栏,那么最简单的方法是将一个栏按钮项从对象检查器拖到右侧导航栏的右侧。然后,您将在您要连接的标头和实现中创建一个 IBAction。

If you have your view controller being created from a NIB file in Interface Builder, and the view controller's design surface has a navigation bar on it, the easiest way to do this would be to drag a bar button item from the objects inspector right onto the navigation bar's right side. You would then create an IBAction in your header and implementation that you would hook up to.

呆° 2024-12-09 08:46:58

我通过直接在 UINavigationBar 中插入按钮来实现此目的:

[yourUINavBar insertSubview:yourButton atIndex:1];

I achieve this inserting the button directly in the UINavigationBar:

[yourUINavBar insertSubview:yourButton atIndex:1];
影子是时光的心 2024-12-09 08:46:58

UIViewController 的 -navigationItem 方法仅与 UINavigationController 或其他 UIViewController 包含一起使用。
您必须访问 UINavigationBar 并直接设置该项目。

UIViewController's -navigationItem method does only work together with UINavigationController or other UIViewController containments.
You will have to get access to the UINavigationBar and set the item directly.

女皇必胜 2024-12-09 08:46:58

如果您想要一个导航栏,并让它按您的预期工作,请使用 UIViewController 作为根视图控制器创建一个 UINavigationController。在您现在使用 UIViewController 的地方使用 UINavigationController。

请查看developer.apple.com 上的 UINavigationController 了解更多详细信息。

If you want to have a navigation bar, and have it work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use that UINavigationController where you are using your UIViewController now.

Check out UINavigationController at developer.apple.com for more details.

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