Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to initialize a view controller from storyboard

I have a view controller I placed in the main storyboard but when I try to initialize and load up the view controller my app crashes. The xcode version I'm using doesn't really tell me the errors I get properly, but I did see that it was giving me a sigabrt signal. I don't know why it isn't working.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SelectDateViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];

    [self presentModalViewController:vc animated:YES];

This is how I called it. All the names I used are correct but I still don't understand why it's crashing.

like image 453
Michael Nana Avatar asked Sep 26 '13 00:09

Michael Nana


1 Answers

Set a breakpoint at the last line to verify vc (and also storyboard) are not nil.

presentModalViewController will crash if its viewController argument is nil.

If both are nil, then your storyboard name is likely incorrect (less likely).

If just vc is nil then the identifier is incorrect (either in code or the storyboard). Make sure the VC's Storyboard ID (as circled in the screenshot) matches your description. It is customary not to choose a class name for this (as you might have more than one instance of a given class in your storyboards).

The Storyboard ID is case sensitive and must be unique. I find the sure fire way to set it is type the name in that field after selecting the view controller and pressing return to end editing (don't just click off the field). I've noticed some corruption occurring in storyboards when converting from Xcode 4 to 5 and vice versa, so diving into the plist/xml might also help.

Storyboard ID

Edit: Also the timing of when this is called is important, see presentModalViewController not working. Make sure the view controller receiving the message has already been shown (i.e. called in viewDidAppear, not viewDidLoad).

As mentioned by Abdullah, you should likely move off the deprecated method to:

[self presentViewController:vc animated:YES completion:nil];

In this case the completion block can be nil without issue, but vc must not be.

like image 130
owenfi Avatar answered Sep 29 '22 13:09

owenfi