216
197

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

awakeFromNibとinitWithFrameとinitWithCoderの使い方

Posted at

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
{
}

メソッドの詳細

参考

216
197
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
216
197

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?