How does a view controller call functionality of a view (how is the link made?)

I have a view controller, I want to action something on the view by calling a method in the view. But I get the error


fatal error: unexpectedly found nil while unwrapping an Optional value


because the view object isnt set.


There are a couple of issues here, firstly I assume I am with MVC "allowed" to control the view from the view controller? Secondarily am I missing some association between the two, maybe on the storyboard? or do I set it up another way...


Sorry for the basic question, but sometimes I get a bit lost


Kind regards

Mark

You have to create these connections in InterfaceBuilder. The following text gives a good explanation (examples in ObjectiveC but works in Swift the same):


https://developer.apple.com/library/prerelease/ios/recipes/xcode_help-IB_connections/chapters/AboutConnectingObjectstoCode.html


And no fear: Cocoa is a big framework and the needed information is sometimes a little hard to find 🙂

A UIViewController doesn't always need to have a view - initially, it starts without one, then the first time you try and query the view it's created lazily by the loadView method. That method sets the UIViewController's view property. After this, the viewDidLoad method is called.


If you don't want to trigger lazy-creation of the view, check UIViewController's isViewLoaded property before trying to talk to the view.


In an MVC sense, the controller lives beyond the view and maintains its state. For example, somebody may push a new view on to the navigation stack and the old view might be removed from memory. The UIViewController sticks around, and the next time anybody needs the view it will recreate it.


That's at least how it used to happen on iOS. Nowadays, UIView objects themselves stick around and maintain their own state, and the thing that gets purged from memory is the internal screen buffer, so once your UIViewController loads its view that view should never be unloaded.

How does a view controller call functionality of a view (how is the link made?)
 
 
Q