Help us understand the problem. What is going on with this article?

awakeFromNibとinitWithFrameとinitWithCoderの使い方

More than 5 years have passed since last update.

initWithFrameとinitWithCoderの違い

  • initWithFrameは、code上でobjectを作る時に呼ばれる
  • initWithCoderは、interface builder(storyboardやnibファイルなど)からobjectを作るときに呼ばれる

initWithCoderとawakeFromNibの違い

  • initWithCoderでは、IBOutletやIBActionはロードされていない、awakeFromNibはロードされた後に呼び出される

使い方

  • interface builder(storyboard)使ってて、なにも考えず使いたいならawakeFromNibを使うといいのでは
  • initWithFrameとinitWithCorder両方呼び出す可能性があるviewの場合は、initWithCorderで書くこととawakeFromNibで書く内容を分けて書く必要がある。
- (void)awakeFromNib
{
    [super awakeFromNib];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

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


- (void)setup
{
}

メソッドの詳細

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away