Simple Example of Using a Delegate in Swift 3
When it came time for me to learn how to use delegates/protocols in swift I understood what they did at a high level (help two objects communicate with each other), but understanding the inner workings and how to create them in code wasn’t coming as easily to me. For me I decided to do a simple project to update a label on one view with text from a textField in another view.
If you’d like to start from scratch the setup is quite simple:
- Two ViewControllers (I’ll be referring to one as SenderVC and one as ReceiverVC)
- ReceiverVC is the initial view controller with a Navigation Bar at the top with one Navigation Item and a UILabel in the center of the view.
- The navigation item should be a segue to the SenderVC which only has a Textfield and a Button.
- That’s it!
If you’re having trouble with that or want to just get right to delegates it I’ve created a branch of the main project as a shell that just has all of the UI elements in place:
Creating the Protocol:
In our SenderVC we want to create a protocol as below:
In this protocol I am declaring a function that MUST be implemented by anyone that want’s to be my (in this case SenderVC’s) delegate and I am also creating a variable called delegate of type UpdateLabelTextDelegate? that will store the object that wants to be my delegate and adhere to my protocol.
The reason delegate is being set as an optional is because if no one wants to be SenderVC’s delegate then the value will be nil.
Becoming the Delegate:
Now that we’ve created the protocol let’s go over to our ReceivingVC and setup our view to become the delegate of SenderVC.
Let’s go through this from top to bottom.
First we added UpdateLabelTextDelegate next to UIViewController. All this is doing is saying that I (ReceiveVC) will adhere to UpdateLabelTextDelegate (the protocol). Now that ReceiveVC has done this it now MUST implement the function updateLabelText which you can see we do.
Going back to the beginning. The objective here is to update the label on ReceiveVC with the textField text from the SenderVC. To do this SenderVC wants to call the protocol function on whomever is the delegate and pass it’s textField text as the argument. Makes sense right? So if the argument coming into the function is going to be the SenderVC’s textField we want to set the label’s text on ReceiverVC to that argument. Which is what you see above in the updateLabelText function.
The last thing we do here is actually set ReceiverVC as the delegate when we segue to the SenderVC.
Sending the Text:
The last thing we need to do is actually send the text. The code for that is below:
As I mentioned in the beginning we have a button on SenderVC to go back to ReceiverVC. Pressing the button is where the magic happens and there isn’t much to it code wise, but let’s step through it a bit to help us understand what’s going on.
Normally you would want to add some code here to check to see if there actually is a delegate and if there actually is text in the textField, but for this simple example we don’t need to.
If you recall in the last step when we segued into SenderVC we set ReceiverVC as the delegate. So we know that ReceiverVC has implemented the updateLabelText function. Here we are simply calling the delegate’s (ReceiverVC’s) updateLabelText function and passing it the argument of the textField’s text.
The last line of code just dismiss the view so we can get back to ReceiverVC.
And that’s it! your label on ReceiverVC should now have the text you entered into the textField on SenderVC. The finished project is located here: