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

查看:244
本文介绍了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];
}


推荐答案

你做的事情很奇怪)

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天全站免登陆