3

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.

Wythe
  • 149
  • 3
  • 12
  • 1
    Views made in a storyboard call initWithCoder: – rdelmar Sep 28 '14 at 05:24
  • possible duplicate of [How to write init method for custom UIView class with xib file](http://stackoverflow.com/questions/21123898/how-to-write-init-method-for-custom-uiview-class-with-xib-file) – rebello95 Sep 28 '14 at 05:25

2 Answers2

2

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. Hope it helps

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Although i have found out how to solve my problem,but this is more than i want,i really helped a lot. – Wythe Sep 28 '14 at 11:52
1

It is in the awakeFromNib: method. Override it in your implementation file like this:

- (void)awakeFromNib {
  [super awakeFromNib];
  // Initialization code
  [self setupStuff];
}
axl411
  • 920
  • 1
  • 7
  • 23