Jak sprawić, by klawisz powrotu na iPhonie zniknął?

Mam dwa UITextFields (np. nazwę użytkownika i hasło), ale nie mogę pozbyć się klawiatury po naciśnięciu klawisza return na klawiaturze. Jak mogę to zrobić?

Author: Julian Król, 2011-05-31

14 answers

Najpierw musisz dostosować się do protokołu UITextFieldDelegate w pliku nagłówkowym View/ViewController Tak:

@interface YourViewController : UIViewController <UITextFieldDelegate>

Potem w Twoim .plik m należy zaimplementować następującą metodę protokołu UITextFieldDelegate:

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

    return YES;
}

[textField resignFirstResponder]; upewnia się, że klawiatura jest oddalona.

Upewnij się, że ustawiasz swój view/viewcontroller jako delegata UITextField po uruchomieniu pola tekstowego w .m:

yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....    
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on
 241
Author: Sid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-13 11:14:43

Zaimplementuj metodę UITextFieldDelegate w następujący sposób:

- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
    [aTextField resignFirstResponder];
    return YES;
}
 19
Author: Nick Weaver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-31 16:03:27

Zobacz Zarządzanie klawiaturą aby uzyskać pełną dyskusję na ten temat.

 8
Author: Caleb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-31 16:06:35

Twoje pola UITextField powinny mieć obiekt delegata (uitextfielddelegate). Użyj poniższego kodu w swoim delegacie, aby klawiatura zniknęła:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
}
Na razie powinno działać...
 6
Author: cschwarz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-31 16:06:49

Wziął mnie kilka prób, miał ten sam problem, to działało dla mnie:

Sprawdź pisownię na -

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

Poprawiłem mój w textField zamiast textfield, pisałem wielką literą "F"... i bingo!! zadziałało..

 6
Author: Ema,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-26 19:57:52

Po naciśnięciu klawisza return, wywołaj:

[uitextfield resignFirstResponder];
 4
Author: Conor Taylor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-31 16:02:34

Po dłuższym czasie szukania czegoś, co ma sens, to jest to, co ułożyłem i zadziałało jak urok.

.h

//
//  ViewController.h
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;

@end

.m

//
//  ViewController.m
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone;
    [_theTextField setDelegate:self];

}

// This part is more dynamic as it closes the keyboard regardless of what text field 
// is being used when pressing return.  
// You might want to control every single text field separately but that isn't 
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}


@end
Mam nadzieję, że to pomoże!
 3
Author: user2904476,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-15 20:08:37

Ustaw delegata pola UITextField do kontrolera ViewController, Dodaj punkt odniesienia między właścicielem pliku a UITextField, a następnie zaimplementuj tę metodę:

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   if (textField == yourTextField) 
   {
      [textField resignFirstResponder]; 
   }
   return NO;
}
 3
Author: Avinash651,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-30 11:44:06

Dodaj to zamiast wstępnie zdefiniowanej klasy

class ViewController: UIViewController, UITextFieldDelegate {

Aby usunąć klawiaturę po kliknięciu poza klawiaturą

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

I aby usunąć klawiaturę po naciśnięciu enter

Dodaj tę linię w viewDidLoad ()

inputField to nazwa użytego pola tekstowego.

self.inputField.delegate = self

I dodać tę funkcję

func textFieldShouldReturn(textField: UITextField) -> Bool {        
        textField.resignFirstResponder()        
        return true        
    }
 3
Author: Ali Kahoot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-29 10:54:09

Swift 2 :

Tak się robi wszystko !

Zamknij klawiaturę przyciskiem Done lub Touch outSide ,Next aby przejść do następnego wejścia.

Najpierw zmień tekst Return Key na Next w Storyboardzie.

override func viewDidLoad() {
  txtBillIdentifier.delegate = self
  txtBillIdentifier.tag = 1
  txtPayIdentifier.delegate  = self
  txtPayIdentifier.tag  = 2

  let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
  self.view.addGestureRecognizer(tap)

}

func textFieldShouldReturn(textField: UITextField) -> Bool {
   if(textField.returnKeyType == UIReturnKeyType.Default) {
       if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
           next.becomeFirstResponder()
           return false
       }
   }
   textField.resignFirstResponder()
   return false
}

func onTouchGesture(){
    self.view.endEditing(true)
}
 2
Author: Mojtabye,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-14 07:34:14

W języku swift powinieneś delegować UITextfieldDelegate , jego ważne nie zapomnij, w kontrolerze viewController, jak:

class MyViewController: UITextfieldDelegate{

     mytextfield.delegate = self

     func textFieldShouldReturn(textField: UITextField) -> Bool {
          textField.resignFirstResponder()
     }
}
 2
Author: Eduardo Oliveros,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-17 19:56:12

Jeśli chcesz zniknąć klawiatura podczas pisania na polu alert textfileds

[[alertController.textFields objectAtIndex:1] resignFirstResponder];
 1
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/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-22 10:48:00

Możesz dodać IBAction do pola uiTextField(Zdarzenie releacji to "Did End On Exit"), A IBAction może nazwać hideKeyboard,

-(IBAction)hideKeyboard:(id)sender
{
    [uitextfield resignFirstResponder];
}

Możesz również zastosować go do innych pól tekstowych lub przycisków,na przykład możesz dodać Ukryty przycisk do widoku, gdy klikniesz go, aby ukryć klawiaturę.

 0
Author: 慭慭流觞,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-03 13:40:20

Możesz wypróbować tę podklasę UITextfield, w której możesz ustawić warunek dynamicznej zmiany interfejsu UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField

 0
Author: CodeInteractive,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-21 00:49:49