2011-05-25 69 views
4

我有一個通過項目嚮導安裝的導航控制器。目前,當應用程序啓動時,導航控制器會自動創建並顯示。iOS - 通過代碼加載導航控制器

我現在需要通過代碼而不是通過.xib魔術來控制導航控制器的顯示。如何禁用自動創建MainWindow.xib/RootViewController.xib?我承認我實際上並不知道發生了什麼事情,並且在嚮導中設置MainWindow.xib和RootController.xib之間的關係。

對此的任何參考或代碼片段將是有益的.. 謝謝!

回答

7

爲了不筆尖創建根導航控制器:

在應用程序委託你應該看到以下內容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    // Add the navigation controller's view to the window and display. 

    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
}

self.navigationController是指從MainWindow.xib中加載導航控制器(這個文件的名字是在你的應用的Info.plist文件中指定的;見下文)。

打開MainWindow.xib並斷開App Delegate的navigationController屬性,然後刪除Objects面板中的導航控制器(不是窗口)對象。

從App Delegate的頭文件中的@property聲明中刪除IBOutlet屬性(因爲它將不再通過nib文件進行連接)。

與大意如下的東西替換應用程序委託代碼:

你可能並不需要:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease]; 
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease]; 

    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
}

爲了不筆尖創建主窗口做這個(我不推薦它),但因爲你(有點)問...

刪除MainWindow.xib。

在main.m中,將UIApplicationMain的最後一個參數替換爲App Delegate的名稱(不帶擴展名)。例如:

int retVal = UIApplicationMain(argc, argv, nil, @"TestProjectAppDelegate");

打開Info.plist文件並刪除下面兩行:

<key>NSMainNibFile</key> 
<string>MainWindow</string>

從應用程序委託的頭文件window @property聲明中刪除IBOutlet中財產。

創建應用程序委託窗口:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    // The rest stays the same... 
}