Storyboard上のビューを以下のようにテストする際、viewDidLoadやviewWillAppearは呼ばれない。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
GHAssertNotNil(storyboard, @"ストーリーボードが存在しない");
self.vc = [storyboard instantiateViewControllerWithIdentifier:@"hoge"]; // Storyboard ID
GHAssertNotNil(self.vc, @"ビューコントローラが存在しない");
[self.vc loadView];
したがって、呼ぶ必要がある場合は明示的に呼ばなければならない。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
GHAssertNotNil(storyboard, @"ストーリーボードが存在しない");
self.vc = [storyboard instantiateViewControllerWithIdentifier:@"hoge"]; // Storyboard ID
GHAssertNotNil(self.vc, @"ビューコントローラが存在しない");
[self.vc loadView];
[self.vc viewWillAppear]; // ここ
[self.vc viewDidLoad]; // ここ
もしくは以下のように、ビューにinitWithCoder:メソッドを生やし、そこにコードを書く。
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
/*
URLデータを読み込む
*/
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"URLs" ofType:@"plist"];
urls = [NSArray arrayWithContentsOfFile:path];
}
return self;
}