1

我完全以编程方式在 iOS 中工作,并且完全使用 IB 工作,但我第一次尝试将两者混合在一起,我感到困惑。我正在编写一个选项卡式应用程序。在我的应用程序委托中,我曾经有以下代码:

//...Code setting view controllers for various tab items and then this...

GHSettingsViewController *svc = [[GHSettingsViewController alloc] init];
svc.tabBarItem.title = @"Settings";
svc.tabBarItem.image = [UIImage imageNamed:@"20-gear-2.png"];
[tabItems addObject:svc];

那很好。

然后我通过 IB 中的故事板创建了一个视图控制器,并向其添加了一堆 UI 元素,并为其提供了以下设置:

在此处输入图像描述

现在我的代码如下:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
GHSettingsViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"settings"];
svc.tabBarItem.title = @"Settings";
svc.tabBarItem.image = [UIImage imageNamed:@"20-gear-2.png"];
[tabItems addObject:svc];

应用程序启动正常,但是当我按下选项卡转到设置屏幕时,我收到以下消息:

在此处输入图像描述

这是什么意思,我该如何解决?

编辑:这是来自应用程序委托的代码的更完整版本didFinishLaunching: { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window makeKeyAndVisible];

NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:5];

GHHaikuViewController *hvc = [[GHHaikuViewController alloc] init];
hvc.tabBarItem.title = @"Home";
hvc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
[tabItems addObject:hvc];

GHComposeViewController *cvc = [[GHComposeViewController alloc] init];
cvc.tabBarItem.title = @"Compose";
cvc.tabBarItem.image = [UIImage imageNamed:@"216-compose.png"];
[tabItems addObject:cvc];

GHWebViewController *wvc = [[GHWebViewController alloc] init];
wvc.tabBarItem.title = @"Buy";
wvc.tabBarItem.image = [UIImage imageNamed:@"80-shopping-cart.png"];
[tabItems addObject:wvc];

GHFeedback *fvc = [[GHFeedback alloc] init];
fvc.tabBarItem.title = @"Feedback";
fvc.tabBarItem.image = [UIImage imageNamed:@"18-envelope.png"];
[tabItems addObject:fvc];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
//UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard.storyboard" bundle:nil]; //Using this instead causes the application to crash on launching.
GHSettingsViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"settings"]; 
//GHSettingsViewController *svc = [storyboard instantiateInitialViewController]; //Using this instead of the line above causes the same error.
svc.tabBarItem.title = @"Settings";
svc.tabBarItem.image = [UIImage imageNamed:@"20-gear-2.png"];
[tabItems addObject:svc];

GHTabBarController *tbc = [[GHTabBarController alloc] init];
tbc.viewControllers = tabItems;
self.window.rootViewController = tbc;

return YES;

}

4

1 回答 1

1

看起来您UIViewControllerUITabBarController. 在application:didFinishLaunchingWithOptions:中,照原样创建新UIStoryboard的。然后创建一个UIViewController实例

UIViewController *viewController = [storyboard instantiateInitialViewController]; 

然后,只需添加viewControllerviewControllers您的UITabBarController.

我不确定这tabItems是做什么用的,但是您需要将视图控制器添加到标签栏的viewControllers数组中。

更新:从我们在聊天中的讨论中,Storyboard 中引用了一个额外的 IBOutlet,它实际上并没有连接并导致崩溃。在调试导航器中点击“继续”按钮给了我们一些额外的信息并导致了问题。

于 2013-01-22T22:30:06.480 回答