带有nib作为子视图的自定义视图似乎没有加载 [英] Custom view with nib as subview doesn't seem to be loading
问题描述
我创建了一个自定义视图,从nib加载其内容,如下所示:
I've created a custom view that loads its content from a nib, like this:
/* PricingDataView.h */
#import <UIKit/UIKIt.h>
@interface PricingDataView : UIView {
UIView *contentView;
}
@property (nonatomic, retain) IBOutlet UIView *contentView;
@end
/* PricingDataView.m */
#import "PricingDataView.h"
@implementation PricingDataView
@synthesize contentView;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[[NSBundle mainBundle] loadNibNamed:@"PricingDataView" owner:self options:nil];
[contentView setFrame:frame];
[self addSubview:contentView];
}
return self;
}
/* ... */
In nib文件我设置 PricingDataView
作为文件所有者的类型,并连接IB中的 contentView
出口。我将接口库中的常规 UIView
放到显示给用户的全尺寸视图中,然后将其类名更改为 PricingDataView
。它都是构建的,但是在运行时,我的自定义视图应该是什么都没有呈现。
In the nib file I set PricingDataView
as the type of the File's Owner, and connected the contentView
outlet in IB. I placed a regular UIView
from the Interface Library onto the full-sized view shown to the user, and then changed it's class name to PricingDataView
. It all builds, but at runtime, nothing is rendered where my custom view is supposed to be.
我把断点放在 PricingDataView.initWithFrame
,但它们没有命中,所以我知道我遗漏了会导致视图被初始化的东西。我很好奇的是,在从nib加载我的其他视图的过程中,所有的初始化都发生在我身上,但不是这个。为什么?
I put breakpoints in PricingDataView.initWithFrame
, but they don't hit, so I know I'm missing something that would cause the view to be initialized. What I'm curious about is that in the process of loading my other views from nibs, all the initialization happens for me, but not with this one. Why?
推荐答案
你确定你的其他观点没有使用 UIViewController
?以下是来自 UIView :
Are you sure your other views aren't using a UIViewController
? Here's a quote from the documentation for initWithFrame:
from UIView
:
如果你使用Interface Builder设计你的接口
,当你的视图对象随后加载
时,这个方法不是
来自nib文件。
nib文件中的对象是
重构,然后使用initWithCoder:方法初始化
,
修改
视图的属性以匹配存储在$中的属性b $ b nib文件。有关如何从nib
文件加载视图的详细信息
,请参阅 资源编程指南 。
这篇关于带有nib作为子视图的自定义视图似乎没有加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!