Matthew Morris
Matthew Morris

Reputation: 333

Delegates VS IBAction for handling events iOS

I am new to swift and new to iOS development. I am currently struggling to understand why delegates are used to handle events that happen in the UI.

So far, in the tutorial I am working through, I have only ever done things like the following code segment to handle UI events:

@IBAction func fahrenheitFieldEditingChanged(_ textField: UITextField) {
    if let text = textField.text, let value = Double(text) {
        fahrenheitValue = Measurement(value: value, unit: .fahrenheit)
    } else {
        fahrenheitValue = nil
    }
}

But now the tutorial I am working through is having me use a delegate to to handle another UI event from the same text field. Why is it done this way? What is the point of using delegates rather than just write actions to handle the events?

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
    let replacementTextHasDecimalSeparator = string.range(of: ".")

    if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil {
        return false
    } else {
        return true
    }
} 

Upvotes: 1

Views: 407

Answers (2)

mfaani
mfaani

Reputation: 36287

Take a look at UITextViewDelegate. It'll become easier to understand.

Responding to Editing Notifications

func textViewShouldBeginEditing(UITextView)

Asks the delegate if editing should begin in the specified text view.

func textViewDidBeginEditing(UITextView)

Tells the delegate that editing of the specified text view has begun.

func textViewShouldEndEditing(UITextView)

Asks the delegate if editing should stop in the specified text view.

func textViewDidEndEditing(UITextView)

Tells the delegate that editing of the specified text view has ended.


Responding to Text Changes

func textView(UITextView, shouldChangeTextIn: NSRange, replacementText: String)

Asks the delegate whether the specified text should be replaced in the text view.

func textViewDidChange(UITextView)

Tells the delegate that the text or attributes in the specified text view were changed by the user.


There is more, but I won't copy/paste anymore you can see yourself. What you see here are things that you can tune in if you want.

If you want you can tune in and get the callback of when the user starts typing. Or you can tune in and get the callback of when the user ended his editing.

How can you find out when the user stoped or started using IBAction?!


Once* you set yourself as a delegate (textViewInstance.delegate = self, you can then choose to get any of these callbacks.

*To be 100% accurate, you need to do that, but also adopt the UITextViewDelegate protocol and then conform to it using the mentioned delegate callbacks

Upvotes: 3

NSAdi
NSAdi

Reputation: 1253

Delegate is a design pattern that allows you to easily customize the behavior of several objects in one central object.

Actions are merely user interactions.

It's not possible to replace delegate callbacks with actions. For instance, if you have a WebView which fails to load, then how does it inform your object using an action? In your example, an action cannot return properties like shouldChangeCharactersIn and replacementString. You need a delegate for that.

Read more... Delegate pattern (Apple docs)

Upvotes: 3

Related Questions