Self Taught Swift: What’s A Delegate?

We’re going to use UIScrollView for this example. In order for our UIScrollView to work we must implement it’s delegate, UIScrollViewDelegate. But before we get into what that does or how to do it let’s put delegates in perspective.

What is a delegate?

The Secretary Analogy: Think of a delegate as a secretary. A secretary alerts the boss when a phone call has come through or if a scheduled meeting has approached. Without the secretary the boss might not ever pick up the phone or the boss could end up at a meeting that they shouldn’t be at. In Swift, a delegate is the secretary. It functions as the secretary between certain objects (UIScrollView in our example) and the boss, your view controller.
class SelfTaughtSwift: UIViewController, UIScrollViewDelegate {
    func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
    }
}

Take a look at those delegate functions in italics. Look at how convenient they are. They do exactly what you think they would do.

  • When a finger begins dragging the scroll view the code in between the braces of scrollViewWillBeginDragging will be executed.
  • When the scroll view stops slowing down, which typically means it came to a stop, the code in between the braces of scrollViewDidEndDecelerating will be executed.
  • When the scroll view scrolls in any way, the code in between the braces of scrollViewDidScroll will be executed.

Those are just a sample of the nine delegate methods available for UIScrollView. There are delegate methods for all of the frameworks supplied by Apple. UITableView, CLLocationManager, UITextField, etc. etc.

In bold we have the actual UIScrollViewDelegate. That spot is where any delegate goes in any type of view controller. Without this here you will not have access to the handy delegate functions that are in italics.

If you’ve never worked with something like a UIScrollView before, the first thing you should do is put the delegate into the file (just like the bold example three paragraphs ago) and then in the body of the view controller type this exactly like you see it:

func scrollView

Xcode will begin to autocomplete what you’re typing and in doing so will show you all of the possible delegate methods you can use on a UITableView.

Once you have your scroll view, or whatever object you want to work with wired into your view controller, make sure you assign it to it’s delegate. If you don’t it can’t talk to it’s delegate. Usually it will be set to self in your view controllers viewDidLoad() method, like this:

class SelfTaughtSwift: UIViewController, UIScrollViewDelegate {
    var thisIsAScrollView = UIScrollView()
    override func viewDidLoad() {
        super.viewDidLoad()
        thisIsAScrollView.delegate = self
    }
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    }
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
    }
}

If you run into a situation where you’ve put an object into your file and you can’t get it to respond when you run your program, the first thing you should do is check to see if it needs a delegate. If it does, put the delegate in it’s correct spot and assign the object to it’s delegate as shown above.

It’s nearly guaranteed that at some point in time you will spend more time than you want to admit trouble shooting a problem that doesn’t exist simply because you forgot to hook up a delegate.

That’s the basic plumbing of a delegate method . We dig a little deeper into delegates and how to get information out of them in Self Taught Swift: The Delegate Behind The Curtain.

If you find a no-frills Swift tutorial helpful hit the heart down below. Cheers!

//** Note that none of the code samples that you see are wired up to be usable. If you put this in your application it will not produce a usable UIScrollView. This is only to show what delegates are all about.