【发布时间】:2016-09-29 17:52:37
【问题描述】:
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)
}
谢谢。我按照建议更改了代码,只收到一个错误:可选类型“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 didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
标签: swift storyboard swift3 viewcontroller xcode8