编译时的Xcode 8表示viewcontroller用标识符简单地更改实例化,实例化视图控制器.我这样做了,为什么会出现两个错误?
我正在使用Swift 3.我想以编程方式更改页面.我已经阅读了很多关于该主题的其他问题.所有这些都使用带有标识符的实例化视图控制器.他们没有采用新语言.
@IBAction func switchPage(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyboard.instantiateViewController("secondViewController") as!
UIViewController
self.presentViewController(secondViewController, animated: true,
completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
谢谢.我按照建议改变了代码,只收到一个错误:可选类型'UIStoryboard?'的值 没有打开; 你的意思是用'!' 要么 '?'?我应该在某处添加感叹号吗?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a
nib.
}
@IBAction func ch(_ sender: UIButton) {
let viewController =
storyboard.instantiateViewController(withIdentifier:
"secondViewController") as! UIViewController
self.present(viewController, animated: true)
}
override func …Run Code Online (Sandbox Code Playgroud)