如何在 xcode 4.2 中添加新选项卡

发布于 2024-12-23 01:13:32 字数 1542 浏览 2 评论 0原文

我使用选项卡视图模板在 Xcode 4.2 中启动了一个项目。在应用程序委托中,我通过代码添加了第三个选项卡,就像第一个和第二个选项卡一样。然后我使用 nib 文件创建了第三个视图控制器类。
当我运行此应用程序时,我会看到所有三个选项卡,但当我单击第三个选项卡时,它崩溃了。 我注意到第一个和第二个 nib 文件在视图的底部有一个黑条(可能代表选项卡栏),但我创建的新的第三个 nib 文件缺少它。知道如何让第三个选项卡工作吗?

谢谢

这就是我添加第三个视图控制器的方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];
    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];
    viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil];
} else {
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil];
    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1,  viewController2, viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

I started a project in Xcode 4.2 using tab view template. In the app delegate I added a third tab by code just like first and second tabs. Then I created a third view controller class with a nib file.
When I run this app, I see all three tabs but when I click on the third tab, it crashes.
I noticed the first and second nib files have a dark bar at the bottom(probably representing the tab bar) of the view but the new third nib file that I created lacks it. Any idea how I make this third tab work?

Thanks

This is how I add the third view controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];
    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];
    viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil];
} else {
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil];
    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1,  viewController2, viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-12-30 01:13:32

在选项卡栏控制器中,确保选项卡的视图控制器类与您创建的视图控制器类相同。

另请检查日志,它可能会为您提供一条非常有用的消息,说明崩溃的原因。

编辑

没关系,您正在为 viewController3 传递一个未初始化的视图控制器。将所有这些初始值设置为零。

In the tab bar controller make sure the class of the view controller for the tab is the same as the view controller class you created.

Also check the logs, it will probably have a very informative message for you as to why it crashed.

Edit:

Never mind, you are passing in an un-initialized view controller for viewController3. Set all of those initial values to nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文