Reputation: 7736
I am trying to set the property of a ViewController: Use Storyboard Id
. You can set it in the storyboard easily:
(Use Storyboard ID is checked)
How can I do this programmatically using Swift? Thanks!
Upvotes: 0
Views: 1444
Reputation: 26
To set the storyboard ID programmatically in Swift, you can use the storyboardIdentifier property of the UIViewController class. Here's how you can do it:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewControllerClass
viewController.storyboardIdentifier = "YourNewStoryboardIdentifier
In the code above, you first instantiate the view controller from the storyboard using the instantiateViewController(withIdentifier:) method, passing in the existing identifier you want to modify. Then, you access the storyboardIdentifier property of the view controller and assign it the new identifier value you want to set.
Make sure to replace "Main" with the actual name of your storyboard file, "YourViewControllerIdentifier" with the identifier of the view controller you want to modify, and "YourNewStoryboardIdentifier" with the new identifier value you want to set.
Note that setting the storyboard ID programmatically does not update the storyboard file itself. It only sets the identifier value during runtime.
Upvotes: 0