0

In the code below, fahrenheitFieldEditingChanged() is called as expected if it has the parameter

textField: UITextField

If I remove the parameter from the function, it doesn't seem to be called, even though I delete the connection from the old function prototype and recreate it for the new one. I know it isn't being called because I added this line at the beginning of the function, and the change doesn't happen no matter what I do:

celsiusLabel.text = "?????"

Please help? Here's my code. (And why do I care about the parameter? I was hoping I could just refer to my IBOutlet textField, which should be the same object as what would be passed to the edit change function, since my storyboard only has one UITextField in it.)

import Foundation
import UIKit

class ConversionViewController: UIViewController {

    @IBOutlet var celsiusLabel: UILabel!
    @IBOutlet var textField: UITextField!

    var fahrenheitValue: Double? {
        didSet {
            updateCelsiusLabel()
        }
    }

    var celsiusValue: Double? {
        if let value = fahrenheitValue {
            return (value - 32) * 5/9
        }
        else {
            return nil
        }
    }

    func updateCelsiusLabel() {
        if let value = celsiusValue {
            celsiusLabel.text = "\(value)"
        }
        else {
            celsiusLabel.text = "???"
        }
    }

    @IBAction func fahrenheitFieldEditingChanged() {
        if let text = textField.text {
            if let value = Double(text) {
                fahrenheitValue = value
            }
            else {
                fahrenheitValue = nil
            }
        }
    }

    @IBAction func dismissKeyboard(sender: AnyObject) {
        textField.resignFirstResponder()
    }

}
7
  • "I know it isn't being called" I don't think you do know that. You have a debugger. Debug! Put a breakpoint right at the start of the body of fahrenheitFieldEditingChanged and see whether you hit it when you type in the field. If you do, it's being called. :)
    – matt
    Commented Mar 3, 2016 at 2:17
  • Good point. I checked in the debugger and now I can say with total certainty that it isn't being called. Commented Mar 3, 2016 at 2:39
  • I've solved it and it turns out the behavior was very particular to my project. Should I delete this question? Commented Mar 3, 2016 at 2:49
  • Hard to advise you intelligently without further info. It might be of general interest, it might not. Can you tell me more about what the problem turned out to be?
    – matt
    Commented Mar 3, 2016 at 2:52
  • Well, now that I look at it a bit more, I'm not sure what has happened. Earlier, celsiusLabel updated with every keystroke in the textField. Now it doesn't update until I dismiss the keypad, and it turns out this new behavior is independent of the existence of the UITextField parameter in my question. Commented Mar 3, 2016 at 3:00

1 Answer 1

2

This has nothing to do with the presence or absence of the parameter (though, I should add, having the parameter is very good practice and you should stick to it, even though you have just the one text field).

Here's what's really going on. As you can see from this screen shot, a text field emits action messages on many different control events:

enter image description here

The one you want to connect to fahrenheitFieldEditingChanged is "Editing Changed". That's the one that fires every times the user changes the text. If you connect a different control event, fahrenheitFieldEditingChanged will be called for that control event, which will not be what you want.

Similarly, if you create the action connection by control dragging to code, the dialog offers to create the connection for the wrong action:

enter image description here

If you don't change Editing Did End to Editing Changed before you hit Connect, bad things will happen down the road.

And unfortunately, when you connect to an existing action method, the dialog doesn't even appear! You just get a connection for the Editing Did End action. That is what happened to you when you disconnected and reconnected.

The moral: use the Connections inspector (my first screen shot) so you can drag from the correct control event in the first place.

1
  • I knew you'd like it. :) Your comments in our earlier discussion gave me just enough clues to guess what you must have done.
    – matt
    Commented Mar 4, 2016 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.