swift 2.0 prepareForSegue 不工作
swift 2.0 prepareForSegue not working
我正在尝试在我制作的应用程序中使用 prepareForSegue
。 prepare forSegue 必须将 class 传递给单独的视图控制器。这是 prepareForSegue
中的代码
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "createPieceSegue" {
var svc = segue.destinationViewController as DestinationViewController!
svc.pieceBaseClass = myClass
}
}
这些是第一个视图控制器继承自的超class类:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate
并且目标视图控制器仅继承自 UIViewController
。我不断收到的错误是:UIViewController is not convertible to'DestinationViewController'
因此我无法在两者之间传递数据。这个错误是什么意思,我该如何解决?
这意味着您还没有在故事板中设置目的地 VC 的自定义 class。所以它只是一个 UIViewController 而不是你的 subclass.
您应该能够使用可选链接。您确定 DestinationViewController 实际上是 UIViewController 的子类吗?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "createPieceSegue" {
if let svc = segue.destinationViewController as? DestinationViewController {
svc.pieceBaseClass = myClass
}
}
我正在尝试在我制作的应用程序中使用 prepareForSegue
。 prepare forSegue 必须将 class 传递给单独的视图控制器。这是 prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "createPieceSegue" {
var svc = segue.destinationViewController as DestinationViewController!
svc.pieceBaseClass = myClass
}
}
这些是第一个视图控制器继承自的超class类:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate
并且目标视图控制器仅继承自 UIViewController
。我不断收到的错误是:UIViewController is not convertible to'DestinationViewController'
因此我无法在两者之间传递数据。这个错误是什么意思,我该如何解决?
这意味着您还没有在故事板中设置目的地 VC 的自定义 class。所以它只是一个 UIViewController 而不是你的 subclass.
您应该能够使用可选链接。您确定 DestinationViewController 实际上是 UIViewController 的子类吗?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "createPieceSegue" {
if let svc = segue.destinationViewController as? DestinationViewController {
svc.pieceBaseClass = myClass
}
}