user3573256
user3573256

Reputation: 219

ViewController.Type does not have a member named view

I have created a new project in Swift, and added a second ViewController to the storyboard, added a class file and linked it. Now I'm trying to access the view of the new vc in the initial vc:

let settingsView = SettingsViewController.view

But it returns the error:

SettingsViewController.Type does not have a member named view

Why is that? How can the view controller not have a view? The view is connected correctly to the VC in the Connections Inspector.

Upvotes: 0

Views: 655

Answers (1)

Sebastian
Sebastian

Reputation: 8164

This is, because you try to access view on the type itself, but not on the instance. That means you have to instantiate the view controller first and after that you can access its members, i.e. (pseudo-swift-code)

let settingsVc = SettingsViewController()
addNewVcToHierarchy(settingsVc)

let settingsView = settingsVc.view

However, usually you do not want to access the view directly from a view controller where it does not belongs to but instead push data into the view controller (e.g. on a segue).

Upvotes: 2

Related Questions