简体   繁体   中英

Swift 3 - No member error after instantiating view controller

I am using Swift 3, Xcode 8.2.

When I instantiate a view controller and try to access one of its variables, I get a: Value of type 'UIViewController' has no member 'someObjectVar'

Here is my code:

let testVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testVC") as UIViewController

testVC.someObjectVar = someObject // error is here

self.present(testVC, animated: true, completion: nil)

The variable in testVC is:

var someObjectVar : someObjectType!

I found this question which has the same issue as me but the solution doesn't work for me since I am not doing the same thing...

Any help would be greatly appreciated.

You are casting it to UIViewController which most definitely does not have that property on it. You need to cast it as whatever UIViewController subclass you made that has that property associated with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM