Issue
I started taking a look of the new Swift
on Xcode 6
, and I tried some demo projects and tutorials. Now I am stuck at:
Instantiating and then presenting a viewController
from a specific storyboard
Objective-C Solution
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myVCID"];
[self presentViewController:vc animated:YES completion:nil];
How to achieve this on Swift?
I would like to suggest a much cleaner way. This will be useful when we have multiple storyboards
1.Create a structure with all your storyboards
2. Create a UIStoryboard extension like this
Give the storyboard identifier as the class name, and use the below code to instantiate
This link has both the implementations:
Swift:
Objective C
This link has code for initiating viewcontroller in the same storyboard
akashivskyy's answer works just fine! But, in case you have some trouble returning from the presented view controller, this alternative can be helpful. It worked for me!
Swift:
Obj-C:
No matter what I tried, it just wouldn't work for me - no errors, but no new view controller on my screen either. Don't know why, but wrapping it in timeout function finally made it work:
It all is a matter of the new syntax, functionality hasn't changed:
If you're having problems with
init(coder:)
, please refer to EridB's answer.Swift 4: