ViewControllerに対するViewクラスは面倒くさがらずに定義したほうがよい。
そうしないとViewControllerのコードが大変なことになるので、どこに何があるか判らなくなる可能性が高くなる。

ViewControllerにViewの制御などを記述した場合

HogeViewController.m
-(void) loadView {
  UIView *view = [[UIView alloc] init];

  _subView = [[UIView alloc] init];

  // サブビューのレイアウト調整
  _subView.frame = CGRectMake(50,50,50,50);

  [view addSubview:_subView];

  self.view = view;
}

処理が複雑になれば、当然、ViewControllerに記述される内容も多くなる。
HogeViewControllerのViewにはHogeViewを対応させ、ViewControllerがViewのインスタンスを持つようにします。
そしてViewControllerでHogeViewのframeを変更していた処理を、ViewのlayoutSubviewsに移動させる。

ViewControllerにViewのインスタンスを持たせる

HogeViewController.m
@implementation HogeViewController

-(id) init {
  if (self = [super init]) {
    // ViewControllerに対応するViewを保持
    _hogeView = [[HogeView alloc] init];
  }
  return self;
}

-(void) loadView {
  self.view = _hogeView;
}

@end
HogeView.m
@implementation HogeView

-(id) init {
  if (self = [super init]) {
    _subView = [[UIView alloc] init];
  }
  return self;
}

-(void) layoutSubviews {
  [super layoutSubviews];

  // レイアウト調整はここでやる
  _subView.frame = CGRectMake(50,50,50,50);
}

@end
最終更新:2014年07月15日 00:13