实例化从XIB加载的Uiview子类



我有一个我从XIB文件加载的Uiview子类。在我的customView.h文件中,我只是忽略了initwithframe中的cgrect参数:方法:

- (id)initWithFrame:(CGRect)frame {
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil] objectAtIndex:0];
    return self;
}

现在我的问题是:每当我实例化我的自定义视图时,我应该通过的框架是什么?

例如,我尝试了:

CustomView *cv = [[CustomView alloc] initWithFrame:CGRectZero];

一个人会假设,由于框架是在xib中设置的,所以这里传递的内容都没关系。但是,这导致我的自定义视图对用户交互无反应。

您可能应该使用initwithCoder

- (id)initWithCoder:(NSCoder*)coder {
    self = [super initWithCoder:coder];
    if (self){
        // Initialization code
    }
    return self;
}

最新更新