Joel Derfner
Joel Derfner

Reputation: 2207

why is this storyboard not instantiating?

I've worked in iOS completely programmatically and I've worked completely with IB, but I'm trying to mix the two for the first time and I'm getting confused. I'm writing a tabbed application. In my app delegate, I used to have the following code:

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

And that was fine.

Then I created a view controller via storyboard in IB and added a bunch of UI elements to it, giving it the following settings:

enter image description here

Now my code reads:

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

The application launches fine, but when I press the tab to go to the settings screen, I get the following message:

enter image description here

What does this mean and how can I fix it?

EDIT: Here's a fuller version of the code from the app delegate in 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;

}

Upvotes: 1

Views: 752

Answers (1)

jmstone617
jmstone617

Reputation: 5707

It looks like you're using this new UIViewController inside of a UITabBarController. In application:didFinishLaunchingWithOptions:, create the new UIStoryboard as you were. Then create a UIViewController instance with

UIViewController *viewController = [storyboard instantiateInitialViewController]; 

Then, just add viewController to the viewControllers array of your UITabBarController.

I'm not sure what tabItems is for, but you need to be adding your view controllers to your tab bar's viewControllers array.

UPDATE: From our discussion in chat, there was an extra IBOutlet referenced in the Storyboard that wasn't actually hooked up and was causing the crash. Hitting the "Continue" button in the debug navigator gave us some extra information and led to the issue.

Upvotes: 1

Related Questions