Using a delegate


#1

Excuse me for my ignorance…I’m a newbie to Swift (and, in fact, programming in general).

In the ’ Using a delegate’ section of the Nerd Ranch IOS Programming book, we use this coding as the first step in accepting a maximum of one decimal in the input text field:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(“Current text: (textField.text)”)
print(“Replacement text: (string)”)
return true
}

I’m having a hard time understanding exactly what this coding is doing. It’s working for me, but I don’t know why. I mean, the function isn’t being called anywhere so how is it even doing anything?

So. I’ve been noodling this and this is what I’ve come up with. Can anyone tell me if I’m even close?

Here it goes:

Earlier in the programming, we declared a variable with the same name as this function (textField) and connected that variable as an outlet on the text field in the storybook. We also set up the view controller that contains this programming as a delegate for that same text field in the storybook.

So is that why this works? Because the outlet from that text field has the same name as this function we just created? And so, the text field is connected as an outlet to a variable named ‘textField’ and now that it knows that the view controller is a delegate so it automatically runs the method with that same variable name?

Am I even close?

Thanks in advance,

Mary


#2

You hit the mark right there. When we conform to a delegate on our view controller, we get access to a set of methods that we can optionally define which adds more functionality to our app. You can think of these as bits of code which are executed in response to certain events which occur in the app, for example when the value of a text field is changed. We don’t manually need to call these methods as this is handled internally.

For instance, the UITextFieldDelegate has a method called textField(_:shouldChangeCharactersIn:replacementString:) which will be called by the system whenever the text of the text field changes. It does nothing unless we tell it to do something. In the tutorial, we add logic to limit the text to one decimal place.


#3

Hi disposedtrolley,

Thank you so much for your response! That definitely helps! I actually didn’t realize that the textfield method was a method of UITextFieldDelegate…so that definitely gives me some clarity on the coding.

Ok, so since the view controller has been set up as a delegate for the text field in Main.storyboard, events in that text field trigger bits of code found in the view controller, specifically bits of code contained in methods that conform to the UITextFieldDelegate protocol. Is that correct?

But what happens if we have two different text fields that the view controller is a delegate for? And we want different bits of code to be executed for each? how would they each know which bit of code they should execute? Or do we have to have two view controllers to have this work?

And how do we know what methods conform to a particular protocol? I’ve tried reading the Apple documentation, but it’s kinda crazy confusing. I’m guessing (hoping) that I’ll learn how to read the documentation eventually, with more experience and practice. Yes?

Thanks again for the help!

Mary