ええと..私がiPhone開発を始めたとき.. rootViewController のことも私をループに陥れました。しかし、それは本当に簡単です。
アプリが起動したら、アプリのデリゲート クラスに UIWindow オブジェクトを作成します。また、そのクラスには、window と呼ばれる UIWindow 型のプロパティがあります。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window=w;
[w release];
// other code here...
}
UIViewController
次に、ウィンドウ階層の最初のビューとなるを作成しview
ます。これは「ルート ビュー コントローラー」と呼ばれます。
紛らわしいのはUINavigationController
、「ルート ビュー コントローラー」として を作成することが多く、そのナビゲーション コントローラーには、スタックに配置する最初のビュー コントローラーである「RootViewController」を要求する init メソッドがあることです。
したがって、ウィンドウは「ルート ビュー コントローラー」を取得しUINavigationController
ます。これは、表示する最初のビュー コントローラーである RootViewController も持っています。
あなたがそれを整理したら、それはすべて理にかなっています..私は思う:-)
ここにすべてを行うコードがあります..(目の前で開いているプロジェクトから取得)
//called with the app first loads and runs.. does not fire on restarts while that app was in memory
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//create the base window.. an ios thing
UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window=w;
[w release];
// this is the home page from the user's perspective
//the UINavController wraps around the MainViewController, or better said, the MainViewController is the root view controller
MainViewController *vc = [[MainViewController alloc]init];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
self.navigationController=nc; // I have a property on the app delegate that references the root view controller, which is my navigation controller.
[nc release];
[vc release];
//show them
[self.window addSubview:nc.view];
[self.window makeKeyAndVisible];
return YES;
}