xcode 如何将导航控制器添加到 xib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13931994/
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-09-15 02:21:17  来源:igfitidea点击:

How do i add add a navigation controller to a xib

objective-ciosxcodecocoa-touch

提问by JSA986

Having only used storyords to subclass a navigation controller before I cant find any tutorial on how to do this for xibs.

在我找不到任何关于如何为 xibs 执行此操作的教程之前,我只使用 storyords 对导航控制器进行子类化。

How do I add a naviagtion controller to a certain xib so I can subclass it? Then I can override the rotation for my view. The part im stuck on is how does the view controller know about the navigation controller? In storyboards I would manually hook it up to my view controller. I just cant work it out with xibs?

如何将导航控制器添加到某个 xib 以便我可以对其进行子类化?然后我可以覆盖我的视图的旋转。我坚持的部分是视图控制器如何知道导航控制器?在故事板中,我会手动将其连接到我的视图控制器。我只是不能用 xibs 解决它?

Ive added a navigation controller next to my views xib...now I need some advice from here.

我在我的视图 xib 旁边添加了一个导航控制器……现在我需要从这里获得一些建议。

Also I've created iPad views from my original iPhone xibs. When I drag the nav controller from IB its still in the iPhone screen size?

我还从我原来的 iPhone xibs 创建了 iPad 视图。当我从 IB 拖动导航控制器时,它仍然是 iPhone 屏幕尺寸吗?

采纳答案by Sam Clewlow

You don't need to set a UINavigationControllerin a xib file the same way you would with storyboards, as a xib only represents one screen. The normal thing to would be alloc/init a UINavigationController, and a UIViewController, and set the view controller as the root of the navigation controller before you display it. This can be done in the App Delegate if you want you whole app wrapped in a navigation controller form the start.

您不需要像UINavigationController使用故事板一样在 xib 文件中设置 a ,因为 xib 仅代表一个屏幕。正常情况是 alloc/init aUINavigationController和 a UIViewController,并在显示之前将视图控制器设置为导航控制器的根。如果您希望整个应用程序从一开始就包含在导航控制器中,则可以在 App Delegate 中完成此操作。

In the app delegate in a non-storyboard app you would want something like this

在非故事板应用程序的应用程序委托中,您会想要这样的东西

self.navigation = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]];

[self.window makeKeyAndVisible];

self.window.rootViewController = self.navigation;

Where self.navigationis a UINavigationControllerproperty or iVar belonging to the App Delegate. If you want to set up the view controller properly in the xib, select the root view in the xib and open the attributes inspector (RHS, the one that looks like a small shield) and choose 'Navigation Bar' in the 'Top Bar' drop down menu. This will display a navigation controller top bar in xib so you can position the other views accordingly.

属于 App Delegateself.navigationUINavigationController属性或 iVar在哪里。如果要在 xib 中正确设置视图控制器,请选择 xib 中的根视图并打开属性检查器(RHS,看起来像一个小盾牌的那个)并在“顶部栏”中选择“导航栏”下拉式菜单。这将在 xib 中显示一个导航控制器顶部栏,以便您可以相应地定位其他视图。

EDIT

编辑

That's not entirely correct, you can create the navigation controller in a xib file, but I've always considered it more hassle than it worth, as there is very few visual elements to it, and they can be set in code very easily.

这并不完全正确,您可以在 xib 文件中创建导航控制器,但我一直认为它比它的价值更麻烦,因为它的视觉元素很少,而且它们可以很容易地在代码中设置。

More info in this tutorial here

本教程中的更多信息在这里

http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/

http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/

回答by Guy Kogus

If you want to override the rotations of the navigation controller, just create a UINavigationController subclass that overrides the rotation methods and then, using P-double's code, instead of calling [UINavigationController alloc]you should be calling something like [MyNavigationController alloc], and it should all work beautifully thanks to the marvels of object-oriented programming.

如果您想覆盖导航控制器的旋转,只需创建一个覆盖旋转方法的 UINavigationController 子类,然后使用 P-double 的代码,而不是调用[UINavigationController alloc]您应该调用类似的东西[MyNavigationController alloc],并且由于面向对象编程的奇迹。