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

公開日: : iPhone, ,

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;	
}

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

関連記事

no image

[iOS SDK] iPhone/iPad はサイレントモードにしても音が鳴る

この前、iOS5 を入れた iPad2 で おんぷちゃん for iPad から音が鳴らない、ほかの

記事を読む

no image

ゲームで起業した人たちのインタビュー集 Gamers at Work

Gamers at Work: Stories Behind the Games Peo

記事を読む

no image

Admob に関して少し調べてみた

iPhoneアプリとAndroidアプリでAdmobを使って広告を表示してみているが、いまだに管理画

記事を読む

no image

APNs のサーバー側をサポートしてくれる Urban Airship (APNs対応その2)

Apple Push Notification Service (APNs) はAppleのAPN

記事を読む

no image

[iOS SDK] モーダル表示したViewController を取得する

UIViewControllerの presentViewController:animated:c

記事を読む

no image

最近読んだ本: エレガントなWeb開発・美しいコードCODA入門 2011/07/15

Coda がとても好きだ。 使っていて気分がいい。おそらく考え抜かれて作られたUIのためだろう。 自

記事を読む

no image

マジック・ツリーハウス 作品名一覧

かいけつゾロリを卒業したうちの小学2年生の娘が今はまっているのがマジック・ツリーハウス。 周りで流

記事を読む

[iOS SDK] iTunes Connect ではまる

iOS8リリースに合わせてiTunes Connect のUIが大幅に変わっており、下記のページの方

記事を読む

Topeak Ridecase for iPhone 7 Plus は2017年1月発売

(画像はiPhone 6 Plus 用 Ridecase)7月にTopeak Ridecase fo

記事を読む

no image

The Presentation Secrets of Steve Jobs ジョブスプレゼン本

自作アプリのテスト中に、たまたまAmazon.comで発見した本をAmazonで注文して当日に届く。

記事を読む

Message

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

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

ポモドーロテクニック用物理タイマーならTime Timer

会社ではなかなか自由に時間を使えないが、家で読書や作業をする

DELL 32インチディスプレイ U3223QE 購入

Dell U3223QE は解像度 3840x216

WWDC 2023 Vision Pro発表

2023/6/5 (日本時間 2023/06/06 2AM)のWWD

M1 MacBook Air を Venturaにアップデートする

M1 MacBook Air を macOS Montere

iOS16でaurioTouch の inBufferFramesが1になる

https://developer.apple.com/librar

→もっと見る

PAGE TOP ↑