Loading Storyboards Programmatically in iOS: A Comprehensive Guide

Loading Storyboards Programmatically in iOS: A Comprehensive Guide

28 February 2025 Stephan Petzl Leave a comment Xcode

Integrating both Storyboards and XIB files in an iOS application can be a challenge, especially when your project initially started with XIBs. If you’re looking to programmatically load and display a Storyboard in your app, this guide will help you achieve that without extensive refactoring.

Step-by-Step Solution

To load a Storyboard programmatically, follow these steps:

  1. Set the Storyboard ID: In your Storyboard, select the view controller you wish to display. Navigate to the Identity Inspector and assign a Storyboard ID to the view controller.
  2. Load and Present the View Controller: Use the following code snippet to load the view controller from the Storyboard and present it:
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"myViewController"];
    viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:viewController animated:YES completion:nil];
                

Swift Implementation

If your project is using Swift, here is how you can achieve the same:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "viewController")
self.navigationController?.pushViewController(viewController, animated: true)
    

For projects that do not use a UINavigationController, replace the push with a present call:

present(viewController, animated: true, completion: nil)
    

Advanced Usage with Swift Enums

For more robust code, consider using enums to manage storyboard names:

enum StoryBoardName {
    case second = "SecondViewController"
}
extension UIStoryboard {
    class func load(_ storyboard: StoryBoardName) -> UIViewController {
        return UIStoryboard(name: storyboard.rawValue, bundle: nil).instantiateViewController(withIdentifier: storyboard.rawValue)
    }
}
    

Load the storyboard in your view controller as follows:

guard let vc = UIStoryboard.load(.second) as? SecondViewController else { return }
self.present(vc, animated: true, completion: nil)
    

Integrating Repeato for Testing

When automating tests for your iOS applications, leveraging tools like Repeato can significantly streamline the process. Repeato allows you to create, run, and maintain automated tests efficiently, supporting both iOS and Android platforms.

With its no-code test automation capabilities, you can quickly record tests using a test recorder. Repeato’s support for data-driven and keyword-driven testing ensures comprehensive test coverage. Additionally, its compatibility with command line scripts and JavaScript code helps automate complex tasks seamlessly.

Unlike other tools that may rely on specific programming languages, Repeato’s approach using computer vision and AI makes it a versatile choice for diverse testing needs. All test data is stored in text and JSON formats, making version control straightforward and efficient. For more information on getting started with Repeato, visit our documentation page.

Like this article? there’s more where that came from!