[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が異なる。
- 「グループとファイル」から、.xib ファイルを全てDeleteキーで削除する
- info.plist から Main nib file base name を項目自体Deleteキーで削除する
- main.m のUIApplicationMain の第4引数の nil を アプリケーションデリゲートクラス名に変更する。
- アプリケーションデリゲートクラスの applicationDidFinishLaunchingXXX で、UIWindow と UIViewController を自力で作成する
- 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; }
うまくいかなかったらお知らせください。
関連記事
-
[iOS SDK] iPhone/iPad はサイレントモードにしても音が鳴る
この前、iOS5 を入れた iPad2 で おんぷちゃん for iPad から音が鳴らない、ほかの
-
ゲームで起業した人たちのインタビュー集 Gamers at Work
Gamers at Work: Stories Behind the Games Peo
-
Admob に関して少し調べてみた
iPhoneアプリとAndroidアプリでAdmobを使って広告を表示してみているが、いまだに管理画
-
APNs のサーバー側をサポートしてくれる Urban Airship (APNs対応その2)
Apple Push Notification Service (APNs) はAppleのAPN
-
[iOS SDK] モーダル表示したViewController を取得する
UIViewControllerの presentViewController:animated:c
-
最近読んだ本: エレガントなWeb開発・美しいコードCODA入門 2011/07/15
Coda がとても好きだ。 使っていて気分がいい。おそらく考え抜かれて作られたUIのためだろう。 自
-
マジック・ツリーハウス 作品名一覧
かいけつゾロリを卒業したうちの小学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
-
The Presentation Secrets of Steve Jobs ジョブスプレゼン本
自作アプリのテスト中に、たまたまAmazon.comで発見した本をAmazonで注文して当日に届く。