Delegation and Scrollview 0707


A very important use of protocols. It’s a way to implement “blind communication “ between a view and its controller.

How it plays out?

A view declares a delegation protocol. What the view wants the controller to do for it.

The view’s API has a weak delegate property whose type is that delegation protocol.

The view use the delegate property to get/do things it can’t own or control on its own.

The controller declares that it implements the protocol.

The controller sets self as the delegate of the view by setting the property.

The controller implements the protocol, probably it has lots of optional methods in it.

Scrollview.delegate = self

It would implement any of the protocol’s methods it is interested in.

The scroll view is an outlet.

If this image settings happened, for example, when someone was preparing me, this would crash. Cuz my scroll view wouldn’t be set in prepare. Now no one is yet segueing to me. If I haven’t set up my scroll view and I get an image, I’m just going to do nothing. Then I better do something when the scroll view gets set.

When the scroll view comes along later and gets set, I’d better do this content size.

Why I’m doing in both places, cuz I’m not sure which is going to happen first.

Don’t forget to set the contentSize. It will not work without minimum/maximum scale being set or delegate method to specify view to zoom.