我完全以编程方式在 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;
}