Where does "view" come from?


#1

So a bunch of places in the ViewController class we’re referencing the view object, such as:

view.addSubview(myFirstLabel)

I’m wondering where this view comes from.

When I option click on view it gives me helptext that says:

var view: UIView { get set }

Can I assume that creating a class of type UIViewController automatically creates an object called view that references the UIView for this class?


#2

Hi cyberk, I think you are correct. If you look at the UIViewController class there is a “var view: UIView” defined there. What I am not sure about is where/how the UIViewController is instantiated. My guess is it’s part of the storyboard?


#3

@cyberk

view is a property from the UIViewController class. When we subclass we get that property and can use it in our code.

Look at the UIViewController class reference to understand what other properties are provided from Apple.

UIViewController Overview:
https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/view

UIViewController view Property:
https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/view

We’ll learn more about the details in the coming week.