From the course: Swift: Delegations and Data Sources

Delegates in iOS code

From the course: Swift: Delegations and Data Sources

Start my 1-month free trial

Delegates in iOS code

- [Lecturer] Now that you have some theory about delegates, let's get practical. Making a delegate to change the background color of this source ViewController. The first step is to define a protocol. I'll do that at the top of my code. You define a protocol the same as a class. Just use protocol instead of a class. So I'll type protocol, and I'll call it ColorChooser and traditionally we put delegate on the end so we know it's a delegate. Then you put your braces. Inside the protocol, will be the skeleton of the function. No code, just the name of the function and any parameters. I'll make one called selected color, that has an integer to represent the color. So going to do func selectedColor, color, and that'll be an Int. Head down to the ColorChooser. In my properties, I'll declare a property delegate and I'll make it the type of my protocol. I'll make it optional so it can be nil when not used. In the chooser, before I dismiss the ViewController, I'll call the selected color method of the delegate, setting the current color as the color of the function. So I'll use delegate and I can call its method selectedColor, and that will be the current color. I've sent the color to the protocol. Next I'll receive it in the source controller. In ViewController, I adopt the delegate by placing the class after ViewController. So go down to ViewController and where it says UIViewController here, you put ColorChooserDelegate, like that. Now Xcode will give me an error. Click on that error to look at it. It says type ViewController does not conform to protocol ColorChooserDelegate. Which is a long way of saying you need to implement that method that we had in ColorChooserDelegate. Now you can have a fix here that says do you want to add the protocol stubs? And I'll go ahead and do that, and there's SelectedColor all ready for some code. Usually I stick this at the bottom, but for this one example I'll stick it at the top 'cause I like my delegates on the bottom of my Viewcontrollers. Inside the function I'll change the count and update the background. Count equals color, and view.backgroundColor equals colors.color, and for the index I'll put color. Now the last step is an important one. When you start with delegates you will forget it. Delegate is currently set to nil. When you try to call a delegate like this you will get an unwrapping nil value error. You must assign a class where the fleshed-out delegate method resides. That class is ViewController. Before I present the chooser I assign delegate to this ViewController, otherwise known as self. So down here, when I present it, I'm going to go here, I'm going to put vc 'cause that's color chooser, and I'm going to pick the delegate and assign it to self. With all that in place, you can run this code. Head over to the live view, click the button, we get our red screen. I'll pick blue, and blue comes back as my background. Same thing with green. You've gone through the steps in code for delegate. Since we've done it once, let's do it one more time for the storyboards, and see the variations.

Contents