从笔尖加载视图 [英] load view from a nib

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

问题描述

我已经为此苦苦挣扎了一段时间,我认为解决方案确实很简单,但是我做对了.我有一个UIViewController,它具有其视图,现在我想为其添加一个子视图.子视图应从笔尖加载.我已按照MyView
4.连接插座
5.在MyViewControllerviewDidLoad方法中:

I've been struggling with this for a while and I think the solution is really simple, but I just can't get it right. I have a UIViewController, which has its view and now I would like to add a subview to it. Subview should be loaded from a nib. I've followed the steps described here, ie.:
1. Create MyView class which is a subclass of UIView
2. Declare IBOutlet properties in MyView
3. Make .xib file, where File Owner is set to UIViewController and View class set to MyView
4. Connect outlets
5. In MyViewController, viewDidLoad method :

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
MyView *mView;
for (id view in nibViews) {
   if ([view isKindOfClass:[MyView class]]) {
      mView = (MyView*) view;   
    }
}
[self.view addSubview:mView];

mView已成功加载,但是当我尝试将其添加为子视图时,我得到EXC_BAD_ACCESS.我什至添加了:mView = [(MyView*) view retain];但这没有帮助.

mView is loaded successfully, but when I try to add it as a subview I get EXC_BAD_ACCESS. I've even added: mView = [(MyView*) view retain]; but that doesn't help.

我在做什么错了?

推荐答案

我不确定您的观点3:

制作.xib文件,其中文件所有者"设置为UIViewController,视图"类设置为MyView

Make .xib file, where File Owner is set to UIViewController and View class set to MyView

文件所有者不应该是uiviewcontroller,而是MyView类

the file owner shouldn't be the uiviewcontroller, but the MyView class

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

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