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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7261766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:20:34  来源:igfitidea点击:

iOS - how to create a navigation bar item in view controller?

iphoneiosuinavigationcontroller

提问by Varundroid

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.

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

Is UINavigationController is the only way to achieve this?

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

Any help would be really appreciated.

任何帮助将非常感激。

Any link to a good tutorial will be great.

任何指向一个好的教程的链接都会很棒。

Thanks.

谢谢。

回答by Kaiser

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

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

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.

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

回答by TommyG

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

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

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

回答by BP.

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.

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

回答by flopes

I achieve this inserting the button directly in the UINavigationBar:

我实现了直接在 UINavigationBar 中插入按钮:

[yourUINavBar insertSubview:yourButton atIndex:1];

回答by Fabian Kreiser

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.

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

回答by Mike Hay

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.

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

Check out UINavigationControllerat developer.apple.com for more details.

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