如何加载XIB? [英] How to load an XIB?

查看:106
本文介绍了如何加载XIB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带2个屏幕的应用程序(MainViewController和AboutViewController)。在用户单击按钮时,我想加载AboutViewController屏幕,该屏幕在另一个XIB中定义。

I have an app with 2 screens (MainViewController and AboutViewController). Upon the user clicking a button, I'd like to load the AboutViewController screen, which is defined in another XIB.

看起来很简单,但我似乎无法找到我的google-fu今天。如何解决此问题?

Seems simple, but I can't seem to find my google-fu today. How do I pull this off?

推荐答案

当您致电 [AboutViewController init] ,它应该调用某种形式的 [super init] ,这是 [UIViewController init] 的同义词。发生这种情况时,视图控制器将自动查找名为(在您的情况下) AboutViewController.xib 的nib文件。如果它找到该文件,它会将它的内容加载到你的视图控制器中。

When you call [AboutViewController init], it's expected to call some form of [super init], which is a synonym for [UIViewController init]. When this happens, your view controller will automatically look for a nib file called (in your case) AboutViewController.xib. If it finds that file, it loads it's contents into your view controller for you.

所以基本上,你需要做的就是初始化你的视图控制器,并确保它与关联的nib文件具有相同的名称。

So basically, all you need to do is initialize your view controller, and make sure it has the same name as the associated nib file.

如果要将具有不同名称的nib文件加载到视图控制器中,可以显式调用 initWithNibName:bundle: ,其中包含您喜欢的任何nib文件的名称。

If you wanted to load a nib file with a different name into your view controller, you could explicitly call initWithNibName:bundle: with the name of whichever nib file you like.

如果标准init (使用同名的nib文件)对你不起作用,你可以查看几件事。

If the standard init (with a same-name nib file) isn't working for you, there are a couple things you could check.


  • 拼写类名与nib文件的拼写(和大小写)相同

  • nib文件包含在项目中,而不仅仅是坐在同一个文件中ory

  • 您的 UIViewController 子类的init方法也会调用 [super init]

  • 您正在调用 UIViewController 子类的init方法

  • 您确实在制作视图控制器查看可见

  • the spelling of the class name is the same as the spelling (and case) of the nib file
  • the nib file is included in the project, and not just sitting in the same directory
  • your UIViewController subclass's init method does also call [super init]
  • you are calling your UIViewController subclass's init method
  • you are indeed making your view controller's view visible

这篇关于如何加载XIB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆