[翻译]  UITextField delegate methods are not fired when setting text programmatically

[CHINESE]  以编程方式设置文本时不会触发UITextField委托方法


I've created a custom input view for entering text into UITextField. Basically it's just custom designed number pad. I have textfields, on which I've set the inputView property to use my custom created UIView subclass. In that view I have collection of buttons - from 0-9 and also backspace.

我已经创建了一个自定义输入视图,用于将文本输入到UITextField中。基本上它只是定制设计的数字键盘。我有textfields,我在其上设置了inputView属性以使用我自定义创建的UIView子类。在那个视图中,我有按钮集合 - 从0-9到退格。

Now I want to change the text of UITextField programmatically when those buttons are tapped. The UITextField adopts UITextInput protocol, which in it's turn adopts UIKeyInput protocol. In that protocol I have all the methods I need, i.e. inserting text into cursor position and deleting text.

现在我想在点击这些按钮时以编程方式更改UITextField的文本。 UITextField采用UITextInput协议,该协议又采用UIKeyInput协议。在该协议中,我拥有我需要的所有方法,即将文本插入光标位置并删除文本。

The problem is that those methods do not fire UITextField delegate methods. That is if I have custom validation in textField:shouldChangeCharactersInRange:replacementString: field for instance, that won't work. I've tried to set the text property of UITextField directly, but that didn't work also.

问题是这些方法不会触发UITextField委托方法。那就是如果我在textField:shouldChangeCharactersInRange:replacementString:field中进行自定义验证,那将无效。我试图直接设置UITextField的text属性,但这也不起作用。

What is the right way of inserting text into UITextField, I mean insert text in a way that all delegate methods would be called?

将文本插入UITextField的正确方法是什么,我的意思是以一种所有委托方法都会被调用的方式插入文本?

3 个解决方案

#1


2  

Set the text of UITextField by calling insertText:

通过调用insertText设置UITextField的文本:

aTextField.insertText(" ")

#2


1  

I tried using textField:shouldChangeCharactersInRange:replacementString:without luck. I kept encountering a "bad selector sent to instance" crash when I tried invoking that method.

我尝试使用textField:shouldChangeCharactersInRange:replacementString:没有运气。当我尝试调用该方法时,我一直遇到“发送到实例的错误选择器”崩溃。

I also tried raising the Editing events, but I still never reached my breakpoint in my ShouldChangeText override of my UITextFieldDelegate.

我也试过提高编辑事件,但我仍然没有在我的UITextFieldDelegate的ShouldChangeText覆盖中到达断点。

I decided to create a helper method which would either invoke the text field's delegate (if exists) or the virtual ShouldChangeCharacters method; and based on that returning true or false, will then change the Text.

我决定创建一个辅助方法,它可以调用文本字段的委托(如果存在)或虚拟的ShouldChangeCharacters方法;并且基于返回true或false,然后将更改Text。

I'm using Xamarin.iOS, so my project is in C#, but the logic below could easily be re-written in Objective-C or Swift.

我正在使用Xamarin.iOS,所以我的项目是在C#中,但下面的逻辑很容易在Objective-C或Swift中重写。

Can be invoked like:

可以调用如:

    var replacementText = MyTextField.Text + " some more text";
MyTextField.ValidateAndSetTextProgramatically(replacementText);

Extension Helper Class:

扩展助手类:

/// <summary>
/// A place for UITextField Extensions and helper methods. 
/// </summary>
public static class UITextFieldExtensions
{
    /// <summary>
    /// Sets the text programatically but still validates
    /// When setting the text property of a text field programatically (in code), it bypasses all of the Editing events. 
    /// Set the text with this to use the built-in validation.
    /// </summary>
    /// <param name="textField">The textField you are Setting/Validating</param>
    /// <param name="replacementText">The replacement text you are attempting to input. If your current Text is "Cat" and you entered "s", your replacement text should be "Cats"</param>
    /// <returns></returns>
    public static bool ValidateAndSetTextProgramatically(this UITextField textField, string replacementText)
    {
        // check for existing delegate first. Delegate should override UITextField virtuals
        // if delegate is not found, safe to use UITextField virtual
        var shouldChangeText = textField.Delegate?.ShouldChangeCharacters(textField, new NSRange(0, textField.Text.Length), replacementText)
                               ?? textField.ShouldChangeCharacters(textField, new NSRange(0, textField.Text.Length), replacementText);
        if (!shouldChangeText)
            return false;

        //safe to update if we've reached this far
        textField.Text = replacementText;
        return true;
    }
}

#3


0  

self.textfield.delegate = self;
    [self.textfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

/* Put Your View controller Object instead self when delegate methods are in another class */

/ *当委托方法在另一个类中时,将您的View控制器对象改为self * /

/* textfield delagete called when text did view change */

/ * textfield delagete在文本查看更改时调用* /

-(void)textFieldDidChange:(UITextField *)textView
{
    if(Condition You want to put)
    {
        //Code
    }
    else
    {
        //Code
    }
}

As par this method you want to make custom Methods too.

与此方法相同,您也想制作自定义方法。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号