如何使用通过Interface Builder创建的nib文件加载UIView [英] How to load a UIView using a nib file created with Interface Builder

查看:106
本文介绍了如何使用通过Interface Builder创建的nib文件加载UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些有点复杂,但应该是可能的东西。所以这里是一个挑战,所有的专家在这里(这个论坛是一个很多你的家伙:))。



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



我这样做的方式是:


  1. 创建Question1View对象作为 UIView 子类,定义一些 IBOutlets

  2. 使用Interface Builder创建 Question1View.xib (这里是我的问题PROBABLY )。我设置 UIViewController UIView 为类Question1View。

  3. 我覆盖 initWithNib

  4. code>的问题管理器视图控制器看起来像这样:

      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if(self = [super initWithNibName:@Question1Viewbundle:nibBundleOrNil]){
    //自定义初始化
    }
    return self;
    }


代码,我收到此错误:


2009-05-14 15:05:37.152 iMobiDines [17148:20b] ***由于未捕获异常 NSInternalInconsistencyException 终止应用程序,原因:' - [UIViewController _loadViewFromNibNamed:bundle:] 问题1查看nib但是没有设置视图出口。'


我确定有一种方法使用nib文件,而不需要创建一个viewController类。

解决方案

还有一种更简单的方法来访问视图,

1 )创建一个自定义的View子类,你想以后有权访问的任何插座。



< --MyView



2 )在UIViewController中加载和处理nib,创建一个IBOutlet属性,视图,例如在MyViewController(一个UIViewController子类)中的



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

(不要忘记综合它并在.m文件中释放)



3)在IB中打开您的nib(我们称为myViewNib.xib),将文件的Owner设置为 MyViewController / p>

4)现在将文件的所有者出口myViewFromNib连接到nib中的主视图。



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

  [[NSBundle mainBundle] loadNibNamed:@myViewNibowner:self options:nil]; 

现在,只要你这样做,调用你的属性self.myViewFromNib从你的笔尖看!


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 :) ).

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.

The way I'm doing it is:

  1. Create Question1View object as a UIView subclass, defining some IBOutlets.
  2. 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.
  3. I link the outlets with the view's component (using IB).
  4. 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] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "Question1View" nib but the view outlet was not set.'

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

解决方案

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

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

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

in MyViewController (a UIViewController subclass)

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

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

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

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

5) Now in MyViewController, write the following line:

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

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

这篇关于如何使用通过Interface Builder创建的nib文件加载UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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