Skip to content

Instantly share code, notes, and snippets.

@jarinosuke
Created January 29, 2014 14:30
Show Gist options
  • Save jarinosuke/8689172 to your computer and use it in GitHub Desktop.
Save jarinosuke/8689172 to your computer and use it in GitHub Desktop.
#import "BadViewController.h"
#define NAVIGATIONBAR_HEIGHT 44.0
#define TABBAR_HEIGHT 49.0
#define WINDOW_WIDTH 320.0
#define WINDOW_HEIGHT_4INCH 568.0
@interface BadViewController ()
@property (nonatomic) NSMutableArray *veryLargeImageLists;
@end
@implementation BadViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)loadView
{
[super loadView];
///(1) ただの UIView を無駄にセットする必要なし、self.view を使う
///(2) self.view にカスタムビューを作成する場合も、frame を計算する必要なし
/// viewWillLayoutSubviews までには適切な frame がセットされている
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:applicationFrame];
view.backgroundColor = [UIColor redColor];
self.view = view;
///(3) データソースなどの初期化は initializer で行う
self.veryLargeImageLists = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"image1"], [UIImage imageNamed:@"image2"], [UIImage imageNamed:@"image3"], nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
///(4) 無闇矢鱈に TabBar や NavigationBar などの高さを定数化して
/// それをもとにレイアウトコードを組まない
/// Container ViewController や UIWindow の状態を決め打ちしたレイアウトコードになってしまう
///(5) そもそもレイアウトコードをここに書かない
UIView *buttonBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(10.0, NAVIGATIONBAR_HEIGHT + 20.0, WINDOW_WIDTH - 20.0, WINDOW_HEIGHT_4INCH - NAVIGATIONBAR_HEIGHT - TABBAR_HEIGHT - 40.0)];
///(6) subview の生成を viewDidLoad で行わない
/// コードの読み辛さはもとより、メモリ警告を受けた際に再ロードする術がない
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
button1.frame = CGRectMake(10.0, 10.0, 100.0, 44.0);
[button1 addTarget:self action:@selector(button1Tapped:) forControlEvents:UIControlEventTouchUpInside];
[buttonBackgroundView addSubview:button1];
[self.view addSubview:buttonBackgroundView];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
#import "GoodViewController.h"
@interface GoodViewController ()
@property (nonatomic) NSMutableArray *veryLargeImageLists;
@property (nonatomic) UIView *buttonBackgroundView;
@property (nonatomic) UIButton *button1;
@end
@implementation GoodViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
///(1) ViewController 内で必ず必要となるデータソースは初期化時にロードしておく
self.veryLargeImageLists = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"image1"], [UIImage imageNamed:@"image2"], [UIImage imageNamed:@"image3"], nil];
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor redColor];
[self.buttonBackgroundView addSubview:self.button1];
[self.view addSubview:self.buttonBackgroundView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
///(2) viewDidLoad 内ではロードした view への追加アクションのみ
[self.button1 addTarget:self action:@selector(button1Tapped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
///(3) 今後の拡張(Universal化、画面回転対応)なども考慮し、レイアウトコードは全てここに。
self.buttonBackgroundView.frame = CGRectInset(self.view.bounds, 10.0, 20.0);
[self.button1 sizeToFit];
self.button1.frame = CGRectOffset(self.button1.frame, 10.0, 10.0);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
///(4) window からデタッチされている場合は self.view を解放。
/// 次回以降アクセスされた時に再び loadView が発火する
if ([self isViewLoaded] && !self.view.window) {
self.view = nil;
}
}
#pragma mark - Views
///(5) subview の初期化は全て getter 内で
/// 経験上、UIViewController を読み解く上で、view の初期化を度々参照する事は少ない
- (UIView *)buttonBackgroundView
{
if (!_buttonBackgroundView) {
_buttonBackgroundView = [UIView new];
}
return _buttonBackgroundView;
}
- (UIButton *)button1
{
if (!_button1) {
_button1 = [UIButton buttonWithType:UIButtonTypeSystem];
}
return _button1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment