2011-09-13 24 views
5

這在Xcode 3中非常容易。但是我完全在Xcode 4中迷失了方向。*它看起來完全沒有使用IB。所有的TabBarController代碼都在代碼中。Xcode 4中沒有mainwindow.xib混淆瞭如何讓我的TabBarController使用NavigationController

問:如何將NavigationBarController添加到Xcode在使用TabBarController模板時生成的默認代碼?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 

UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

self.tabBarController = [[UITabBarController alloc] init]; 

self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 

self.window.rootViewController = self.tabBarController; 

[self.window makeKeyAndVisible]; 

return YES; 

} 

回答

6

正如有人提到你可以添加一個xib文件配置應用程序來使用它。下面是如果你決定走這條路線,它總是最好知道無論哪種方式

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

    UIViewController *viewController1 = [[FirstViewController alloc] init]; 
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
    [viewController1 release]; viewController1 = nil; 

    UIViewController *viewController2 = [[SecondViewController alloc] init]; 
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
    [viewController2 release]; viewController2 = nil; 

    self.tabBarController = [[UITabBarController alloc] init]; 

    NSArray *viewController = [[NSArray alloc] initWithObjects:navigationController1, navigationController2, nil]; 
    [navigationController1 release]; navigationController1 = nil; 
    [navigationController2 release]; navigationController2 = nil; 

    self.tabBarController.viewControllers = viewControllers; 
    [viewControllers release]; viewControllers = nil; 

    self.window.rootViewController = self.tabBarController; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

這是寫在瀏覽器,但它應該工作的代碼版本。

+0

嗨,哇,兩個答案都是正確的。我現在正在使用代碼路由。 –

6

您可以手動添加的MainWindow.xib文件(新文件 - >空Interface Builder的文件),然後在你的應用程序的Info.plist,你可以添加一個名爲「主筆尖文件基本名稱」鍵,設置它的值改爲「MainWindow」。

在您的應用程序委託中,將窗口和UINavigationController設置爲IBOutlets,並刪除生成它們的代碼。然後在你的MainWindow.xib文件中添加一個應用程序委託的實例,UINavigationController和一個Window。將UINavigationController和Window連接到代理的出口。