Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which init method for UIView will be called when using drag and drop in xib file

here is the custom collection view,and below label is the (rating view i design).
enter image description here

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

        [self initGrayStarView];

        [self initYellowStarView];

        [self initRatingLabel];
    }
    return self;
}

but i find that it every time the rating view didn't appear(image view and label did). then i found it's because that i didn't call the init method for rating view,so it's nil ,but why?
this is the init method.

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

like image 291
Wythe Avatar asked Jan 20 '26 11:01

Wythe


1 Answers

Consider this sort of pattern...

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

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

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

Try to "know what is happening" but that will get you through if you're just getting started.

like image 174
Fattie Avatar answered Jan 22 '26 06:01

Fattie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!