2

I am taking an online iOS course provided by Stanford. In the sample code,

@IBOutlet weak var scrollView: UIScrollView! {
    didSet {
        scrollView.contentSize = imageView.frame.size
        // all three of the next lines of code
        // are necessary to make zooming work
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.03
        scrollView.maximumZoomScale = 1.0
    }
}

However, if I remove scrollView.delegate = self, this scroll view still works on the simulator.

My questions:

  1. Is it necessary to set scrollview.delegate as self? Why or why not?
  2. What does self refer to? command + left click locate "did set".
Jill Clover
  • 2,168
  • 7
  • 31
  • 51

3 Answers3

3
  1. You do not have to set the delegate for scrollView. You only do it if you want to react delegate methods that scrollView calls. For example, if you want to do something when user performs a scroll, you need to set the delegate and implement func scrollViewDidScroll(scrollView: UIScrollView) method. Everytime user scrolls, you will be able to react in this method's body.

  2. Self refers to the class that holds this variable. In this case it will be probably your UIViewController

Eluss
  • 512
  • 3
  • 5
3

Define, it still works? I mean if you can move it with touch, yeah it will work.

The reason for

scrollView.delegate = self

is that it allows you to add code which can execute upon scroll began, scroll ended etc. That way you have customization points to alter the behavior or actions of scrollview. Without that little code the delegate code will never get called.

Makes sense?

Sam B
  • 27,273
  • 15
  • 84
  • 121
  • The logic behind: user gesture -> the delegate -> scroll view zooming. Is it correct? Why not user gesture -> scroll view zooming? – Jill Clover Sep 06 '16 at 22:06
  • Why not scrollview include such method? For flexibility? Is it because if we include everything, we will make scrollview too massive? – Jill Clover Sep 06 '16 at 22:14
  • For zoom feature, I take it back. There is an excellent answer by Dan - http://stackoverflow.com/questions/3657451/how-to-enable-zoom-in-uiscrollview – Sam B Sep 06 '16 at 22:28
  • I read his answer twice and am looking into the source code, but I am not confidence to say I am clear with my question (including the delegate methods in scrollview). – Jill Clover Sep 06 '16 at 22:41
1

What does self refer to?

self is the object that's executing the code that refers to self. The scrollview instance variable defined in your code snippet is part of some class; in this case it's surely a view controller. The difference between an object and a class is like the difference between a peach pie and a recipe for peach pie: the recipe is a specification that tells you all about peach pies, but you can't eat the recipe, whereas an actual peach pie is a distinct thing, and you can make several similar pies from a single recipe. Anyway, self is the name of the specific object that's executing the didSet method associated with the scrollview variable.

Is it necessary to set scrollview.delegate as self? Why or why not?

A UIScrollView object has a delegate property the you can set to any object that knows how to be a scroll view's delegate, i.e. any object that implements the UIScrollViewDelegate protocol. But since views are almost always managed by a view controller of some sort, it's pretty typical to have the view controller that manages the scroll view also act as it's delegate. The scroll view will work just fine with no delegate at all; setting up a delegate is only important if you want to do something in response to changes in the scroll view, or otherwise modify its behavior somehow. So, if a scroll view has a delegate, it's typically the view controller that manages that scroll view, and since it's probably also the view controller that sets up the scroll view in the first place self (meaning the view controller) is what you'll see most of the time.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I like your analogy. In "The difference between an object and a class ", do you mean "an instance and a class"? – Jill Clover Sep 06 '16 at 22:57
  • Exactly -- an object is an instance of a class. I took some trouble to explain it because beginners sometimes don't get that there's a difference, and it can be hard to convey that `self` refers to a specific instance. – Caleb Sep 06 '16 at 23:06