[iPhone SDK] View Based Application で NIB(XIB)ファイルを削除してみる

iPhoneアプリ開発に慣れてくると、段々Interface Builderを使わなくなってくる。
Interface Builder との連携を完全に切ろうと思い、今回初めてView Based で作成したプロジェクトで NIB(XIB)ファイルを削除してみたらはまったのでメモしておく。

Stack Overflowでも、同じようにはまっている人がいた。(what is the difference between loadView and viewDidLoad?)

NIBファイルを削除すると、NIBファイルが生成してくれていた、UIWindowやUIViewControllerを自力で作らなければならなくなり、UIApplicationMain の変更も必要になる。
さらに、UIViewController はデフォルトではNIBファイルを読みにいってしまう(実際には、self.view に値がないとNIBファイルを探しに行くようだ) ので、それを避けるためにloadView でUIView を作成する必要がある。

参考にしたのは UIKit詳解リファレンスのP.21-P.27あたり。

UIKit詳解リファレンスは Window Based なプログラムを改造しているが、自分は View Based で作ったものを改造したので、loadViewのところではまってしまった。

具体的には、下記のステップの4,5が異なる。

  1. 「グループとファイル」から、.xib ファイルを全てDeleteキーで削除する
  2. info.plist から Main nib file base name を項目自体Deleteキーで削除する
  3. main.m のUIApplicationMain の第4引数の nil を アプリケーションデリゲートクラス名に変更する。
  4. アプリケーションデリゲートクラスの applicationDidFinishLaunchingXXX で、UIWindow と UIViewController を自力で作成する
  5. UIViewController クラスのloadViewで、self.view に UIView を自力で作成して入れる

手順をもう少し詳しく書いてみる。

View Based Application のテンプレートを使い、 NoXibTest という名前のプロジェクトを作成

「グループとファイル」から、MainWindow.xibとNoXibTest2ViewController.xibを削除

main.m の UIApplicationMain を下記のように変更

int main(int argc, char *argv[]) {
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"NoXibTestAppDelegate");
    [pool release];
    return retVal;
}

アプリケーションデリゲートクラスの applicationDidFinishLaunchingXXX で、UIWindow と UIViewController を自力で作成する

	
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	CGRect rect = [[UIScreen mainScreen]bounds];
	window = [[UIWindow alloc]initWithFrame:rect];
	viewController = [[[NoXibTestViewController alloc]init]autorelease];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

UIViewController クラスのloadViewで、self.view に UIView を自力で作成して入れる

- (void)loadView {
	CGRect screenBounds = [[UIScreen mainScreen]bounds];		
	UIView *view = [[[UIView alloc]initWithFrame:screenBounds]autorelease];
	view.backgroundColor = [UIColor redColor]; // わかりやすいように赤背景にする
	self.view = view;	
}

うまくいかなかったらお知らせください。

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.