Trying to initialize a view controller from storyboard
Asked Answered
B

3

8

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.

Brockie answered 26/9, 2013 at 0:31 Comment(2)
When you created the app, did you enable storyboards to begin with?Causerie
@foriinrangeawesome Yeah yeah I did.Brockie
H
6

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.

Hamid answered 26/9, 2013 at 0:59 Comment(0)
O
0

[self presentModalViewController:vc animated:YES]; is deprecated.

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

Offcolor answered 26/9, 2013 at 0:35 Comment(1)
That could very well be the reason. I wonder why it does not give him a warning though.Causerie
C
0

Did you set the initial ViewController from Storyboard? If yes then Make sure the your Storyboard id is same. If you are using storyboard you can directly present or dismiss the viewController no need to write the code for it

hope this help you :)

Climatology answered 26/9, 2013 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.