Creating and displaying ViewController in Swift: complete guide
Hello everyone! I'm new to Swift and iOS development, transitioning from Objective-C. I have a problem with creating and displaying a new ViewController programmatically (without Storyboard). In Objective-C I just did:
```objc
MyViewController *vc = [[MyViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
```
But in Swift it doesn't work as simply. I tried:
```swift
let vc = MyViewController()
self.present(vc, animated: true, completion: nil)
```
But nothing happens — the screen just doesn't appear. I've read that you need to use Storyboard or XIB, but I need to do everything programmatically. I also tried:
```swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MyVC")
```
No answers yet.
Log in to leave an answer