UITextField “value changed” not firing when field is updated

杀马特。学长 韩版系。学妹 提交于 2019-12-31 10:57:27

问题


I have an IBAction that I have connected to a UITextField element in Interface Builder. (Firing on "value changed" event)

I also have a UISlider that updates the TextField element automatically when it's value gets adjusted.

When I run my app, the TextField does get updated when I adjust the slider, but the "value changed" event does not fire when this happens. The value changed event also does not fire when I edit the TextField by hand as well. (Although using the Did Editing End trigger does cause it to fire).

My question is, how can I get the value changed trigger to fire for both a programmatic update of the TextField as well as when the user adjusts the value.


回答1:


As suggested by TheRonin, use UITextFieldDelegate and implement the following method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string



回答2:


Don't use UIControlEventValueChanged for UITextField. What you want is UIControlEventEditingChanged.




回答3:


To get EditingChanged textfield event, I suggested add selector as

[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

As textfield value changed, the changed value you will get from this textChanged: selector.

-(void)textChanged:(UITextField *)textField
{
    NSLog(@"textfield data %@ ",textField.text);
}



回答4:


IF you are assigning a delegate to the text field then note that (when you return NO from the delegate method below, the event (Editing changed) won't be triggered. Consider invoking the event listener when you do return NO from this method

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string



回答5:


An old question, but the first search result I found, and none of the answers are satisfactory. I found this solution to work best: Swift UITextField subclass handle text change programmatically.

Override UITextField's text property to add a didSet(), and call your value changed function from there.

  override open var text: String? {
        didSet {
            myValueDidChange()
        }
    }



回答6:


Swift 4:

class YourViewController: UIViewController {
    // MARK: Outlets
    @IBOutlet weak var titleTextField: UITextField!

    // MARK: View lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        titleTextField.delegate = self
    }
}

// MARK: UITextFieldDelegate methods
extension YourViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        print("changed")
        return true
    }
}



回答7:


While value gets assigned programmatically, in that case, textFieldDidChange never get called because actually textField hasn't changed. For this external event needs to pass

eg., textfield.sendActions(for: .editingChanged) this will fire the editing changed event and textFieldDidChange will get called even after value changed programmatically.

Where to apply this event:

After assigning value to the text filed eg., textfield.text = "some value"




回答8:


For Swift 3:

textField.addTarget(self, action: #selector(textChanged), for: .valueChanged)

func textChanged() { 
 .... 
}


来源:https://stackoverflow.com/questions/8224221/uitextfield-value-changed-not-firing-when-field-is-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!