簡體   English   中英

以編程方式設置文本時不會觸發UITextField委托方法

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

我已經創建了一個自定義輸入視圖,用於將文本輸入到UITextField 基本上它只是定制設計的數字鍵盤。 我有textfields,我在其上設置了inputView屬性以使用我自定義創建的UIView子類。 在那個視圖中,我有一些按鈕 - 從0-9到退格。

現在我想在點擊這些按鈕時以編程方式更改UITextField的文本。 UITextField采用UITextInput協議,該協議采用UIKeyInput協議。 在該協議中,我擁有我需要的所有方法,即將文本插入光標位置並刪除文本。

問題是這些方法不會觸發UITextField委托方法。 那就是如果我在textField:shouldChangeCharactersInRange:replacementString: field中進行自定義驗證,那將無效。 我試圖直接設置UITextField的text屬性,但這也不起作用。

將文本插入UITextField的正確方法是什么,我的意思是以一種所有委托方法都會被調用的方式插入文本?

通過調用insertText設置UITextField的文本:

aTextField.insertText(" ")

我嘗試使用textField:shouldChangeCharactersInRange:replacementString:沒有運氣。 當我嘗試調用該方法時,我一直遇到“發送到實例的錯誤選擇器”崩潰。

我也試過提高編輯事件,但我仍然沒有在我的UITextFieldDelegate的ShouldChangeText覆蓋中到達斷點。

我決定創建一個輔助方法,它可以調用文本字段的委托(如果存在)或虛擬的ShouldChangeCharacters方法; 並根據返回true或false,然后將更改文本。

我正在使用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;
    }
}
self.textfield.delegate = self;
    [self.textfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

/ *當委托方法在另一個類中時,將您的View控制器對象改為self * /

/ * textfield delagete在文本查看更改時調用* /

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

與此方法相同,您也想制作自定義方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM