为什么这个故事板没有实例化?

时间:2013-01-22 22:20:56

标签: ios xcode storyboard

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

//...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元素,给它以下设置:

enter image description here

现在我的代码是:

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

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

enter image description here

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

编辑:以下是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;

}

1 个答案:

答案 0 :(得分:1)

您似乎在UIViewController内使用了这个新的UITabBarController。在application:didFinishLaunchingWithOptions:中,按原样创建新的UIStoryboard。然后使用

创建一个UIViewController实例
UIViewController *viewController = [storyboard instantiateInitialViewController]; 

然后,只需将viewController添加到viewControllers的{​​{1}}数组中。

我不确定UITabBarController的用途,但您需要将视图控制器添加到标签栏的tabItems数组中。

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