Attempt to initialize view controller from storyboard

I have a view controller that I put in the main storyboard, but when I try to initialize and load the view controller, my application crashes. The xcode version that I use does not really tell me about the errors that I am getting properly, but I saw that it gave me a cigar signal. I do not know why it does not work.

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

This is what I called it. All the names I used are correct, but I still don't understand why this is a failure.

+8
ios objective-c
source share
3 answers

Set a breakpoint on the last line to check vc (as well as storyboards) is not zero.

presentModalViewController will fail if its viewController argument is zero.

If both values ​​are zero, your storyboard name is most likely incorrect (less likely).

If only vc is nil, then the identifier is incorrect (either in the code or in the storyboard). Make sure the VC Storyboard ID (as shown in the screenshot) matches your description. Usually there is no choice of a class name for this (since you may have several instances of this class in your storyboards).

The storyboard identifier is case sensitive and must be unique. I believe that the right way to install it is to enter a name in this field after selecting the view controller and clicking the "return to end" button (do not just click on the field). I noticed that some corruption occurs in storyboards when converting from Xcode 4 to 5 and vice versa, so help with plist / xml can also help.

Storyboard id

Edit: It is also important when this is called, see presentModalViewController does not work . Make sure that the view controller receiving the message has already been shown (i.e. called in viewDidAppear, not viewDidLoad).

As mentioned by Abdullah, you should probably abandon the deprecated method in order to:

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

In this case, the termination block can be nil without problems, but vc should not be.

+6
source share

[self presentModalViewController:vc animated:YES]; outdated.

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

0
source share

Have you installed the initial ViewController from the Storyboard? If so, make sure your storyboard identifier is the same. If you use a storyboard, you can directly represent or reject the viewController, there is no need to write code for it

hope this helps you :)

0
source share

All Articles