Search code examples
iosobjective-cstoryboardscene

Loading storyboard scene through code


I'm very new to storyboards and objective-c in general. Basically, I have a welcome screen with 4 buttons on it. Each button loads a new scene via the modal way. One of the screens requires an internet connection. I have already setup the function to check to see if there is internet. If there is no connection, I'd like it to load a separate scene. It looks like this (webView is the name of my UIWebView):

-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {
//code
}

Now, the only issue I'm having is how to load the scene that it should direct to in this function. I have the feeling that I need to start by creating an outlet for this specific scene, but I'm unsure of how to even do that. I tried searching for a guide on how to do it, but it is all going over my head since I'm so new to programming. Any help is greatly appreciated!


Solution

  • If you are talking about UIViewController when you are saying scene the there is a rely easy way to do it, just call the following in your desired method (off course with the required name changes)

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil];
    YourViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
    

    Now make sure that your storyboard has a UIViewController of type YourViewController and that UIViewController has YourViewControllerIdentifier as identifier set in the storyboard.

    Now if you are talking about a custom UIView when you are saying scene then you can't do that, you can't load a custom UIView from a sotryboard without loading the proper view controller. But you can create a custom UIView with a .xib file and load it on your desired methods.