Swift 3,Xcode 8实例化视图控制器不起作用 [英] Swift 3, Xcode 8 Instantiate View Controller is not working

查看:116
本文介绍了Swift 3,Xcode 8实例化视图控制器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 8编译时说用标识符将实例化viewcontroller更改为简单地实例化视图控制器.我这样做了,为什么会给出两个错误?

Xcode 8 when it compiles says to change instantiate viewcontroller with identifier to simply, instantiate view controller. I did that, why does it give two errors?

我正在使用Swift3.我想以编程方式更改页面.我已经阅读了很多关于该主题的其他问题.它们全部使用带有标识符的实例化视图控制器.他们还没有采用新的语言.

I'm working with Swift 3. I want to change pages programmatically.I've read a lot of other questions on the topic. All of them use instantiate view controller with the identifier. They haven't adopted the new language.

@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?"的值没有包装;你的意思是使用'!'或者 '?'?我应该在某个地方添加一个感叹号吗?

Thanks. I changed the code as suggested, and receive only one error: Value of optional type 'UIStoryboard?' not unwrapped; did you mean to use '!' or '?'? Should I add an exclamation point somewhere?

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.
}



}

推荐答案

尝试这样.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"secondViewController") as! UIViewController
self.present(viewController, animated: true)    

这篇关于Swift 3,Xcode 8实例化视图控制器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆