2011-05-30 12 views
11

En un UITextView para ocultar el teclado, no es el método:Añadir un botón para ocultar el teclado

... 
    textfield.returnKeyType = UIReturnKeyDone; 
    textfield.delegate = self; 
.... 

-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 

} 

pero si quiero dejar el botón "Done" para el "retorno" y añadir un botón para esconder el teclado, ¿cómo puedo?

Respuesta

34

Puede asignar una barra de herramientas con un botón que descarta el teclado como el campo de texto inputAccessoryView. Un ejemplo rápido sería

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease]; 
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease]; 
toolbar.items = [NSArray arrayWithObject:barButton]; 

textField.inputAccessoryView = toolbar; 
+0

gracias, pero si quiero llamar también de un método clave? – Vins

+0

No te entendí. ¿Cómo quieres llamar? –

+0

Tengo un método que guarda el texto en una base de datos [self saveString]; – Vins

3

¡Esto se puede hacer de manera más fácil!

Hice una vista personalizada en el IB, en mi viewController.h acabo de hacer un IBOutlet UIView *accessoryView;, conectados y un - (IBAction)dismissKeyboard;

pongo en la vista de una barra de herramientas con un botón de hecho, hice una conexión con el IBAction un escribió: [textView resignFirstResponder] y

- (void)viewDidLoad 
{ 
    textView.inputAccessoryView = accessoryView; 
    [super viewDidLoad]; 
} 

Pero en realidad que se parece un poco extraño y no de manzana al estilo ... ¿Tienes una idea? Versión

4

Swift 2.0:

//Declared at top of view controller 
var accessoryDoneButton: UIBarButtonItem! 
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44)) 
//Could also be an IBOutlet, I just happened to have it like this 
let codeInput = UITextField() 

//Configured in viewDidLoad() 
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:))) 
self.accessoryToolBar.items = [self.accessoryDoneButton] 
self.codeInput.inputAccessoryView = self.accessoryToolBar 

Swift 4:

//Declared at top of view controller 
var accessoryDoneButton: UIBarButtonItem! 
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)) 
//Could also be an IBOutlet, I just happened to have it like this 
let codeInput = UITextField() 

//Configured in viewDidLoad() 
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed)) 
self.accessoryToolBar.items = [self.accessoryDoneButton] 
self.codeInput.inputAccessoryView = self.accessoryToolBar 

func donePressed() { 
    //Causes the view (or one of its embedded text fields) to resign the first responder status. 
    view.endEditing(true) 
} 

UIToolBar Documentation

'inputAccessoryView' documentation

+0

¿Dónde está el método 'donePressed'? – rmaddy

+0

' donePressed' es sólo una función en su ' UIViewController'. Por lo general, renuncio al teclado activo del primer respondedor y luego saco el controlador de vista de la pila de navegación si así lo deseo. –

Cuestiones relacionadas