iPhone - 从 nib 文件加载 UIView? [英] iPhone - Load a UIView from a nib file?

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

问题描述

我正在继承 UIView 试图从 nib 文件加载我在界面构建器中删除的视图.我在返回行上收到以下错误:

I am subclassing UIView trying to load the view i have dropped in interface builder from a nib file. I get the following error on the return line:

由于未捕获的异常NSGenericException"而终止应用,原因:此编码器要求从 initWithCoder 返回替换的对象:"

- (id)initWithCoder:(NSCoder *)aDecoder
{
    [super initWithCoder:aDecoder];
    NSArray *objects = [[NSBundle mainBundle]
                        loadNibNamed:@"MyView" 
                        owner:nil 
                        options:nil];
    if (self = [objects objectAtIndex:0])
    {
    }
    return [self retain];
}

推荐答案

你在做一些很奇怪的事情)

You are doing something very strange)

loadNibNamed:owner:options: 将调用 initWithCoder: 从 xib 实例化您的视图.但是您正在从 initWithCoder: 调用 loadNibNamed:owner:options:.无限递归?

loadNibNamed:owner:options: will call initWithCoder: to instantiate your view from xib. But you are calling loadNibNamed:owner:options: from initWithCoder:. Infinite recursion?

要从 xib 加载视图,您可以执行以下操作:

To load view from xib you can do the next:

@implementation MyView

+ (MyView*) myView
{
  NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
  return [array objectAtIndex:0]; // assume that MyView is the only object in the xib
}

@end

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

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