Ios – Get StoryBoard from ViewController identifier


I'm working with 3 storyBoards and in a section of code I get the identifier of a ViewController but can't call instantiateViewControllerWithIdentifier because I dont know which storyBoard belongs to it.

The question is: Is it possible to get the StoryBoard instance/identifier using the ViewController identifier?

Best regards!

Best Solution

You can call instantiateViewControllerWithIdentifier but you will get a nil value returned if the view controller with that identifier does not exist.

You could test this way on your three storyboards until you find a value different than nil.

See Apple documentation

Edited: after detecting the exception (sorry, Apple docs are a bit confusing sometimes), what you can do is wrap you code with a try/catch block like this

    @try {
        UIViewController *myViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myViewControllerID"];
    @catch (NSException *exception) {
        DLog(@"Exception: This is not the storyboard");
    @finally {
        DLog(@"I found it!");