ios UITextField“值已更改”不会在字段更新时触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8224221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:31:07  来源:igfitidea点击:

UITextField "value changed" not firing when field is updated

objective-cioscocoa-touchinterface-builder

提问by Andrew Lauer Barinov

I have an IBActionthat I have connected to a UITextFieldelement in Interface Builder. (Firing on "value changed" event)

我有一个IBAction已连接到UITextFieldInterface Builder 中的元素。(触发“值改变”事件)

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

我还有一个UISlider在值被调整时自动更新 TextField 元素。

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).

当我运行我的应用程序时,当我调整滑块时,TextField 确实会更新,但在发生这种情况时不会触发“值已更改”事件。当我手动编辑 TextField 时,值更改事件也不会触发。(虽然使用 Did Editing End 触发器确实会导致它触发)。

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.

我的问题是,如何让值更改触发器为 TextField 的编程更新以及用户调整值时触发。

采纳答案by Ilanchezhian

As suggested by TheRonin, use UITextFieldDelegateand implement the following method

根据 TheRonin 的建议,使用UITextFieldDelegate并实现以下方法

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

回答by Mark Adams

Don't use UIControlEventValueChangedfor UITextField. What you want is UIControlEventEditingChanged.

不要UIControlEventValueChanged用于UITextField. 你想要的是UIControlEventEditingChanged.

回答by Kirti Nikam

To get EditingChanged textfield event, I suggested add selector as

为了获得EditingChanged textfield event,我建议将选择器添加为

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

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

随着文本字段值的更改,您将从该textChanged:选择器中获得更改的值。

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

回答by Nech

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

如果您将委托分配给文本字段,请注意(当您从下面的委托方法返回 NO 时,将不会触发事件(编辑更改)。当您从此方法返回 NO 时,请考虑调用事件侦听器

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

回答by robert

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.

一个老问题,但我找到的第一个搜索结果,没有一个答案令人满意。我发现这个解决方案效果最好:Swift UITextField 子类以编程方式处理文本更改

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

覆盖 UITextField 的 text 属性以添加一个 didSet(),并从那里调用您的值更改函数。

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

回答by balazs630

Swift 4:

斯威夫特 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
    }
}

回答by Akshay Digrase

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

虽然值以编程方式分配,但在这种情况下,textFieldDidChange 永远不会被调用,因为实际上 textField 并没有改变。对于这个外部事件需要通过

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

例如,textfield.sendActions(for: .editingChanged)这将触发编辑更改事件,并且即使在以编程方式更改值后也会调用 textFieldDidChange 。

Where to apply this event:

在哪里申请此事件

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

为文本字段赋值后,例如textfield.text = "some value"

回答by UnRewa

For Swift 3:

对于Swift 3

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

func textChanged() { 
 .... 
}