5

这在 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;

}
4

2 回答 2

6

您可以手动添加 MainWindow.xib 文件(New File -> Empty interface builder document),然后在您的应用程序 Info.plist 中添加一个名为“Main nib file base name”的键并将其值设置为“MainWindow”。

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

于 2011-09-13T20:41:05.413 回答
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;
}

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

于 2011-09-13T20:55:07.347 回答