oimo’s blog

エンジニア(♀)のブログです

StoryboardのViewControllerからXibを読み込む時

awakeFromNib = IBOutletやIBActionがロードされた後に呼び出す(初期化処理)
initWithCoder   = IBOutletやIBActionがロードされていない
initWithFrame   = Objectを作る時に呼ばれる( code )
initWithCoder   = Objectを作る時に呼ばれる( IB , Storyboard )

++++++++++++++++++++++
実装Aと実装Bの違いってなあに?
++++++++++++++++++++++

【実装A】

◎xibのUIVew 実装ファイル

- (id)initWithNib {
    UINib *nib = [UINib nibWithNibName:@"firstView" bundle:[NSBundle mainBundle]];
    self = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    return self;
}

◎StoryboardのUIViewController

NSArray *firstViewObject = [[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil];
UIView *firstView = [firstViewObject objectAtIndex:0];
firstView.frame = CGRectMake(0, 0, homeScrollView.width, firstView.height);
[homeScrollView addSubview:firstView];

 

【実装B】

◎xibのUIVew ヘッダーファイル
+ (instancetype)homeFirstView;

◎xibのUIVew 実装ファイル
+ (instancetype)homeFirstView {
 ① UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
 id homeFirstView = [nib instantiateWithOwner:self options:nil][0];
 return homeFirstView;
}

◎StoryboardのUIViewController

HomeFirstViewBgView *homeSlideBannerXib = [HomeFirstViewBgView homeFirstView];

 

① xibファイルからUIViewControllerインスタンスを得る

 

【備忘録メモ】

A-Liaison BLOG: [UIView willMoveToSuperview:] が便利です

- (void)willMoveToSuperview:(UIView *)newSuperview

- (void)didMoveToSuperview