ios - 使用 swift 在不同的 View Controller 之间切换

标签 ios swift

我想在不同的 View Controller 之间切换,这是我的代码,

let sb = UIStoryboard(name:"Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("tabBarController") as ViewController 
self.presentViewController(vc, animated: true, completion: nil)

'tabBarController' 是我在标识符检查器中写的 Storyboard ID。但我在这一行遇到了一些错误。

let vc = sb.instantiateViewControllerWithIdentifier("tabBarController") as ViewController 

这是截图, swift_dynamicCastClassUnconditional

有什么问题?

还有一个问题,这是sb初始化后的截图,storyboardFileName是“Main.storyboardc”,不应该是“Main.storyboard”吗?

enter image description here
非常感谢!!!!

最佳答案

错误表明 instantiateViewControllerWithIdentifier 返回的 View Controller 无法转换为 ViewController。

这可能是因为您没有将 Storyboard 中 View Controller 的 Custom Class 属性设置为 ViewController。

转到 Storyboard,选择 View Controller ,然后打开身份检查器并查找自定义类属性。将其设置为 ViewController 并按 Tab 键退出该字段以确保它可以使用。然后再次运行。

关于ios - 使用 swift 在不同的 View Controller 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28445804/

相关文章:

ios - 什么是 CGFloat.random(min : CGFloat, max : CGFloat) of swift 2 in swift 3?

ios - 是否可以从 Websheet.app 内部打开到 Safari 的链接

Swift 4 委托(delegate)和传递文本字段数据

ios - 使用 Swift 的 Lumberjack 2.0 记录器

ios - UIAlertController 与 NSObject 重用次数更多

ios - CAShapeLayer 不可见

ios - AVMetadataFaceObject 偏航角反转/相反取决于设备型号

ios - 检查tableView每一行的textField

swift - 将数据从 Firebase 传输到自定义信息窗口

ios - 消除项目中的错误