Ios 以编程方式设置文本时,不会激发UITextField委托方法

Ios 以编程方式设置文本时,不会激发UITextField委托方法,ios,objective-c,uitextfield,uitextfielddelegate,Ios,Objective C,Uitextfield,Uitextfielddelegate,我创建了一个自定义输入视图,用于将文本输入到UITextField。基本上它只是定制设计的数字键盘。我有文本字段,在这些字段上我设置了inputView属性以使用自定义创建的UIView子类。在这个视图中,我有一组按钮——从0到9,还有退格 现在我想在点击这些按钮时以编程方式更改UITextField的文本。UITextField采用协议,而协议反过来又采用协议。在该协议中,我拥有所有需要的方法,即将文本插入光标位置并删除文本 问题是这些方法不会激发UITextField委托方法。例如,如果我在

我创建了一个自定义输入视图,用于将文本输入到
UITextField
。基本上它只是定制设计的数字键盘。我有文本字段,在这些字段上我设置了inputView属性以使用自定义创建的
UIView
子类。在这个视图中,我有一组按钮——从0到9,还有退格

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

问题是这些方法不会激发
UITextField
委托方法。例如,如果我在
textField:shouldchangeCharactersRange:replacementString:
字段中进行自定义验证,那么这将不起作用。我曾尝试直接设置
UITextField
的text属性,但也不起作用

UITextField
中插入文本的正确方法是什么,我的意思是以调用所有委托方法的方式插入文本

self.textfield.delegate = self;
    [self.textfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
/*当委托方法位于另一个类中时,将视图控制器对象改为self*/

/*文本视图发生更改时调用textfield delagete*/

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

此方法同样需要自定义方法。

< P>我尝试使用<代码> TeXField:SubdCuxEngultSimule:ReaPrimeStudio:<代码>没有运气。当我尝试调用该方法时,我不断遇到“发送到实例的错误选择器”崩溃

我还尝试引发编辑事件,但在UITextFieldDelegate的ShouldChangeText覆盖中,我仍然没有达到断点

我决定创建一个helper方法,它要么调用文本字段的委托(如果存在),要么调用虚拟ShouldChangeCharacters方法;根据返回的真或假,将更改文本

我使用的是Xamarin.iOS,所以我的项目是C#,但是下面的逻辑可以很容易地用Objective-C或Swift重新编写

可以这样调用:

    var replacementText = MyTextField.Text + " some more text";
MyTextField.ValidateAndSetTextProgramatically(replacementText);
扩展助手类:

/// <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;
    }
}
//
///UITextField扩展和帮助器方法的位置。
/// 
公共静态类UITextFieldExtensions
{
/// 
///以编程方式设置文本,但仍进行验证
///以编程方式(在代码中)设置文本字段的文本属性时,它会绕过所有编辑事件。
///将文本设置为使用内置验证。
/// 
///正在设置/验证的文本字段
///您试图输入的替换文本。如果您当前的文本为“Cat”,并且您输入了“s”,则替换文本应为“Cat”
/// 
公共静态bool以编程方式验证数据集(此UITextField textField,string replacementText)
{
//首先检查现有委托。委托应覆盖UITextField虚拟现实
//如果未找到委托,则可以安全地使用UITextField virtual
var shouldChangeText=textField.Delegate?.ShouldChangeCharacters(textField,新NSRange(0,textField.Text.Length),replacementText)
??textField.ShouldChangeCharacters(textField,新NSRange(0,textField.Text.Length),replacementText);
如果(!shouldChangeText)
返回false;
//安全地更新,如果我们已经达到这一步
Text=replacementText;
返回true;
}
}

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

aTextField.insertText(" ")

请添加您的代码。。。如果您使用的是UITextField的setText:of,委托方法将不会被调用。我认为这包括了它:我认为不需要代码,因为它只是一行-textField.text=@“myText”;是的,它没有像我在问题中所说的那样调用委托,所以我问这个问题是为了找到解决方法。不幸的是,它没有。我尝试了这些示例,但委托方法仍然无法启动。好的,您知道要插入的文本,因此您应该能够在插入之前验证它,从而减少对委托的需要。听起来不是一个很难解决的问题。@Alexey实际上不应该,因为这也不会触发委托方法
textField.insertText(“对我也适用!”)