aircraft aircraft - 1 month ago 9
iOS Question

Q: create a vc from sb.instantiateViewController(withIdentifier:) cannot use its property in swift

Create a

vc10
from
sb.instantiateViewController(withIdentifier:)
,can not use its
property
even convert its type to
ViewController10
.

this below is my code:

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

Answer

You should convert it to ViewController10 when you init it like:

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