A simple build time script that scans all of the storyboards in your project, and builds a struct of each, each containing an enum of all available view controllers.
- Python
- lxml module (
pip install lxml
)
- Add the python script to your project root
- Grab the blank R.swift provided in this repo and add it to your project
- Add a run script in the project settings > Build Phases > plus button
- In the script text, paste the following:
python "${SRCROOT}/ios-storyboards.py" "${SRCROOT}/"
- The script generate a file named R+storyboards.swift in the root folder of the project, open Finder, and drag the file to Xcode project navigator.
And you should be set to go! The R+storyboards file is recreated on every build, so if you remove/add/change id of storyboard, the compiler will let you know, rather then finding out on runtime 😄
- in the storboard file, select the view controller you want to access
- go to identity inspector, and add a unique name to the storyboard in the storyboard ID, under identity. Remember that the way the script works is by allowing access to a every storyboard file, and on every file, allow access to every controller, which identified by the Storyboard ID
- Now you can access it! Let's say the storyboard file is: Main.storyboard, and the controller we want has the id: helloworld1, you can generate the UIViewController with this simple line:
let vc: UIViewController = R.storyboard.Main.helloworld1^ // where the '^' character initialaze the controller