问:从sb.instantiateViewController创建一个vc(withIdentifier :)不能在swift中使用它的属性

时间:2016-10-27 03:00:03

标签: ios swift storyboard type-conversion

vc10创建sb.instantiateViewController(withIdentifier:),无法使用其property甚至将其类型转换为ViewController10

以下是我的代码:

vc9:

let sb = UIStoryboard.init(name: "Main", bundle: nil)
var vc10 = sb.instantiateViewController(withIdentifier: "ViewController10")

//vc10.delegate = self // there is the error: Value of type 'UIViewController' hs no member 'delegate'
(ViewController10(vc10)).delegate = self // this did not work too 

self.navigationController!.pushViewController(vc10, animated: true) 

vc10:

var delegate:ViewController9?  // this is the property delegate

1 个答案:

答案 0 :(得分:1)

您应该在初始化时将其转换为ViewController10

var vc10 = sb.instantiateViewController(withIdentifier: "ViewController10") as! ViewController10