在Swift中创建和显示ViewController的完整指南
大家好!我是Swift和iOS开发的新手,从Objective-C转过来。我在以编程方式(不使用Storyboard)创建和显示新的ViewController时遇到了问题。在Objective-C中,我只需这样做:
```objc
MyViewController *vc = [[MyViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
```
但在Swift中,这并不那么简单。我尝试了:
```swift
let vc = MyViewController()
self.present(vc, animated: true, completion: nil)
```
但什么也没发生——屏幕根本没有显示。我读到需要使用Storyboard或XIB,但我需要完全以编程方式实现。我还尝试了:
```swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MyVC")
self.present(vc, animated: true)
```
但收到错误,说identifier未找到。请告诉我如何在Swift中正确创建和显示ViewController?也许我遗漏了什么?我使用的是Xc