¿Cómo ocultar el teclado cuando presiono la tecla return en un UITextField?


Al hacer clic en un campo de texto aparece el teclado. ¿Cómo lo oculto cuando el usuario presiona la tecla return?

Author: piperchester, 2010-08-26

9 answers

Primero haga que su archivo sea delegado para UITextField y luego agregue este método a su código ..

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}
 255
Author: Saurabh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-26 09:58:24

En viewDidLoad declarar:

[yourTextField setDelegate:self];

Luego, incluya la anulación del método delegado:

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
 59
Author: oscar castellon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-10 22:25:41

En swift hacer así:
Primero en su ViewController implementar esto UITextFieldDelegate Por ejemplo.

class MyViewController: UIViewController, UITextFieldDelegate {
....
}

Ahora agregue un delegado a un TextField en el que desea descartar el teclado cuando se toca return en el método viewDidLoad como se muestra a continuación o donde lo está inicializando. Por ejemplo.

override func viewDidLoad() {

    super.viewDidLoad()   
    myTextField.delegate = self
}

Ahora agregue este método.

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}
 12
Author: Homam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-29 06:12:53

Prueba esto en Swift ,

Paso 1: Establecer delegado como uno mismo a su textField

textField.delegate = self

Paso 2: Agregue este UITextFieldDelegate debajo de su declaración de clase,

extension YourClassName: UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
         textField.resignFirstResponder()
        return true
    }
}
 10
Author: Zaid Pathan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-10 15:11:37

Set delegate of UITextField, and over ride, textFieldShouldReturn method, in that method just write following two lines:

[textField resignFirstResponder];
return YES;

Eso es todo. Antes de escribir un código no se olvide de establecer el delegado de un UITextField y establecer el tipo de clave de retorno a "Hecho" desde la ventana de propiedades.(comando + mayús + I).

 1
Author: Matrix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-10 09:11:07

Puede conectar "Acción primaria Activada" (haga clic derecho en UITextField) con una IBAction y puede renunciar al primer respondedor (sin delegación). Ejemplo (Swift 4):

@IBAction func textFieldPrimaryAction(_ sender: UITextField) {
    sender.resignFirstResponder()
    ...
}
 1
Author: Ferenc Kiss,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-05 18:19:11

Prueba esto,

[textField setDelegate: self];

Luego, en el método delegado TextField

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
 1
Author: Vineesh TP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-10 15:11:26
[textField resignFirstResponder];

Usa esto

 0
Author: DURGESH Chaurasiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-22 10:48:09

Swift 4

Establece el delegado de UITextField en view controller, field.delegate = self, y luego:

extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // don't force `endEditing` if you want to be asked for resigning
        // also return real flow value, not strict, like: true / false
        return textField.endEditing(false)
    }
}
 0
Author: dimpiax,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-29 22:53:59