1
votes

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;

}

1
Please edit your question to include the stack trace of the signal.rob mayoff
Did you check wether it's the correct storyboard?lukaswelte
Are you using a tab segue? If so, you shouldn't be instantiating a new view controller, you should be using the prepareForSegue method. You can refer to it with GHSettingsViewController *svc = (GHSettingsViewController*)[segue destinationViewController];Aaron Brager
rob, I'm afraid I don't know how to find that. If you let me know I'll add it to the question right away. Lukas, the storyboard is named Storyboard.storyboard and it's the only storyboard I have. Aaron, I'm not confident I understand what a tab segue is but just tried your substitution and got an "use of undeclared identifier: segue" error, so I assume I'm not using one.Joel Derfner
I've edited my code to show a fuller version of didFinishLaunching.Joel Derfner

1 Answers

1
votes

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.