OGeek|极客世界-中国程序员成长平台

标题: ios - 在 xib 文件中使用拖放时将调用 UIView 的哪个 init 方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:16
标题: ios - 在 xib 文件中使用拖放时将调用 UIView 的哪个 init 方法

这里是自定义 Collection View ,标签下方是(我设计的评级 View )。
enter image description here

    - (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self initGrayStarView];

        [self initYellowStarView];

        [self initRatingLabel];
    }
    return self;
}

但我发现每次评分 View 都没有出现( ImageView 和标 checkout 现)。然后我发现是因为我没有调用评级 View 的init方法,所以它是nil,但是为什么呢?
这是init方法。

but it never call it ,it only call the layoutSubview method.



Best Answer-推荐答案


考虑这种模式...

-(id)initWithFrameCGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _setup];
    }
    return self;
}

-(id)initWithCoderNSCoder *)aCoder {
    if (self = [super initWithCoder:aCoder]) {
        [self _setup];
    }
    return self;
}

-(void)_setup {
    blah;
    blah;
    blah;
}

尝试“了解正在发生的事情”,但如果您刚刚开始,这将帮助您度过难关。希望对你有帮助

关于ios - 在 xib 文件中使用拖放时将调用 UIView 的哪个 init 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26081893/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4