Xcode 4中没有mainwindow.xib混淆了如何让我的TabBarController使用NavigationController

Edw*_*ter 5 iphone tabbarcontroller

这在Xcode 3中非常简单.但是我完全迷失在Xcode 4中.*看起来根本就没有使用IB.并且所有TabBarController代码都在代码中.

问题:如何在使用TabBarController模板时将NavigationBarController添加到Xcode生成的默认代码中?

- (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;

}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ick 6

您可以手动添加MainWindow.xib文件(新文件 - >空接口构建器文档),然后在您的应用程序Info.plist中添加一个名为"主nib文件基本名称"的键,并将其值设置为"MainWindow".

在您的app委托中,将窗口和UINavigationController设置为IBOutlets并删除生成它们的代码.然后在MainWindow.xib文件中添加app delegate,UINavigationController和Window的实例.将UINavigationController和Window连接到委托的出口.


Pau*_*l.s 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;
}
Run Code Online (Sandbox Code Playgroud)

这是在浏览器中编写的,但它应该可以工作.