Not sure what you actually want to do within BecomeFirstResponder
, but without creating a custom subclass of UITextField
, you can create a custom UITextFieldDelegate
and assign it to the UITextField.Delegate
property.
If you are trying to prevent the field from becoming a first responder, you could use the UITextFieldDelegate.ShouldBeginEditing
to return false
, this method is called before BecomeFirstResponder
is called) and UITextFieldDelegate.EditingStarted
is called after BecomeFirstResponder
is called:
public class TextFieldDelegate : UITextFieldDelegate
{
public override bool ShouldBeginEditing(UITextField textField)
{
return base.ShouldBeginEditing(textField);
}
public override void EditingStarted(UITextField textField)
{
base.EditingStarted(textField);
}
}
Refer to the following link to review the full order/flow of all the method calls of UITextFieldDelegate
:
Ref: https://developer.apple.com/reference/uikit/uitextfielddelegate
Update:
Here is how I do the SecureTextEntry
toggle / No clear field on edit:
1) Custom UITextFieldDelegate
:
public class SecureTextFieldDelegate : UITextFieldDelegate
{
public override void EditingStarted(UITextField textField)
{
if (textField.SecureTextEntry != true)
{
var text = textField.Text;
textField.DeleteBackward();
textField.InsertText(text);
}
}
}
2) Setup your UITextField:
uiTextField.Delegate = new SecureTextFieldDelegate();
3) Handle Touch of toggle and/or physical keyboard field Tabbing:
uiSwitch.ValueChanged += (object sender, EventArgs e) =>
{
uiTextField.SecureTextEntry = !uiTextField.SecureTextEntry;
if (uiTextField.SecureTextEntry == true)
{
var text = uiTextField.Text;
uiTextField.DeleteBackward();
uiTextField.InsertText(text);
}
};
The UITextField.Text
now should never clear unless the user performs a delete