如何使用使用 Interface Builder 创建的 nib 文件加载 UIView

人气:897 发布:2022-10-16 标签: iphone ios objective-c cocoa-touch interface-builder

问题描述

我正在尝试做一些复杂的事情,但应该是可能的.所以这里对你们所有的专家来说都是一个挑战(这个论坛有很多人:)).

I'm trying to do something a bit elaborate, but something that should be possible. So here is a challenge for all you experts out there (this forum is a pack of a lot of you guys :) ).

我正在创建一个问卷组件",我想将它加载到 NavigationContoller(我的 QuestionManagerViewController)上.组件"是一个空"的UIViewController,可以根据需要回答的问题加载不同的视图.

I'm creating a Questionnaire "component", which I want to load on a NavigationContoller (my QuestionManagerViewController). The "component" is an "empty" UIViewController, which can load different views depending on the question that needs to be answered.

我的做法是:

将 Question1View 对象创建为 UIView 子类,定义一些 IBOutlets.创建(使用 Interface Builder)Question1View.xib (这就是我的问题可能在哪里).我将 UIViewControllerUIView 都设置为 Question1View 类.我将 outlet 与视图的组件链接(使用 IB).

我将 QuestionManagerViewControllerinitWithNib 覆盖为如下所示: Create Question1View object as a UIView subclass, defining some IBOutlets. Create (using Interface Builder) the Question1View.xib (HERE IS WHERE MY PROBLEM PROBABLY IS). I set both the UIViewController and the UIView to be of class Question1View. I link the outlets with the view's component (using IB).

I override the initWithNib of my QuestionManagerViewController to look like this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:@"Question1View" bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

当我运行代码时,我收到了这个错误:

When I run the code, I'm getting this error:

2009-05-14 15:05:37.152 iMobiDines[17148:20b] *** 由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:-[UIViewController _loadViewFromNibNamed:bundle:] 加载了Question1View" nib,但未设置视图出口.'

2009-05-14 15:05:37.152 iMobiDines[17148:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "Question1View" nib but the view outlet was not set.'

我确信有一种方法可以使用 nib 文件加载视图,而无需创建 viewController 类.

I'm sure there is a way to load the view using the nib file, without needing to create a viewController class.

推荐答案

还有一种更简单的方法可以访问视图,而不是将 nib 作为数组处理.

There is also an easier way to access the view instead of dealing with the nib as an array.

1) 创建一个自定义 View 子类,其中包含您以后想要访问的任何插座.--我的视图

1) Create a custom View subclass with any outlets that you want to have access to later. --MyView

2) 在要加载和处理 nib 的 UIViewController 中,创建一个 IBOutlet 属性来保存加载的 nib 的视图,例如

2) in the UIViewController that you want to load and handle the nib, create an IBOutlet property that will hold the loaded nib's view, for instance

在 MyViewController(一个 UIViewController 子类)中

in MyViewController (a UIViewController subclass)

  @property (nonatomic, retain) IBOutlet UIView *myViewFromNib;

(不要忘记合成它并在你的 .m 文件中发布)

(dont forget to synthesize it and release it in your .m file)

3) 在 IB 中打开您的 nib(我们称之为myViewNib.xib"),将文件的所有者设置为 MyViewController

3) open your nib (we'll call it 'myViewNib.xib') in IB, set you file's Owner to MyViewController

4) 现在将文件的 Owner outlet myViewFromNib 连接到 nib 中的主视图.

4) now connect your file's Owner outlet myViewFromNib to the main view in the nib.

5) 现在在 MyViewController 中,编写以下行:

5) Now in MyViewController, write the following line:

[[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];

现在,只要您这样做,调用您的属性self.myViewFromNib"即可让您从您的 nib 访问视图!

Now as soon as you do that, calling your property "self.myViewFromNib" will give you access to the view from your nib!

369