[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 Developer Program 更新のため Developer Certificates を更新
この前iOS Developer Programを更新したが、1年経ったので Developer C
-
[英単語学習法] MyShortcuts を使って通知センターからいろいろな辞書を引いてみる
MyShortcuts+Viewer 価格: ¥100
-
「サンプルプログラムでマスターする iPhone SDK」 の感想
2009年9月に発売された本。 iPhone SDK 3.0 リリース後に書かれた本なので、3.
-
Heroku + Ruby + Sinatra 本 : Cloning Internet Applications with Ruby
Cloning Internet Applications with RubyChang Sau S
-
Apple Special Event 2015
https://www.gizmodo.jp/2015/09/applelive_event_ip
-
[iOS SDK] iPad 専用アプリをiPhoneでも使えるようにする
iPad 初代やiPad2の頃は、iPhoneとiPadの画面サイズがかなり異なり、また
-
MacOS版1Password を3.8.22から 5.4.1にアップグレード
3.xからの1Password ユーザで、MacOS版もiOS版も有料版を3.xの頃に購入して、
-
iPhone Core Audio プログラミング (Extended Audio File Services)
発売日からずっと気になる存在だったが、Audio Queue Services を使ったプログラムを
-
Core Graphics と Quartz
Core Graphics と Quartz の関係がよく分からなかったので調べていたところ、ほぼ同
-
iOS Developer Program 更新(2回目)
4月はじめにiOS Developer Program の期限が切れるので、Apple から