Example of UITextField ResignFirstResponder with Monotouch
Asked Answered
D

1

5

I'm a newbie. I can't figure out how and where to call ResignFirstResponder to get rid of the keyboard when the user finished entering the text in an UITextField. I'm a bit confused by the UIResponder class. Mono documentation says: "To dismiss the keyboard, send the UIResponder.ResignFirstResponder message to the text field that is currently the first responder." How to do so? Can someone post a simple working example? There are many examples in Obj-C but none in C#. Many thanks.

Dewaynedewberry answered 16/3, 2010 at 11:31 Comment(0)
H
17

Here's an example I've done recently:

private UITextField _textField;

public override void ViewDidLoad()
{
    _textField = new UITextField();
    _textField.Text = "King Alfonso III";
    _textField.Bounds = bounds;
    _textField.Placeholder = "Username";
    _textField.ShouldReturn = delegate
    {
        _textField.ResignFirstResponder();
        return true;
    };
    View.AddSubview(_textField);
}

Also if you are submitted a form with a button, make sure you resign all the textfields in the button click, to avoid getting responder errors.

public void ButtonClick(object sender, EventArgs e)
{
    _textField.ResignFirstResponder();
    // All other textboxes

    // Other button logic
}
Herdsman answered 16/3, 2010 at 12:51 Comment(2)
Top mate! I feel quite n00by :) Thanks again Chris!Dewaynedewberry
@kentakhy Glad to help, it took me about 3 hours to figure it out from small bits on the web and Miguel's examples: github.com/migueldeicaza/monotouch-samplesHerdsman

© 2022 - 2024 — McMap. All rights reserved.