How to make return key on iPhone make keyboard disappear?

后端 未结 14 456
有刺的猬
有刺的猬 2020-12-02 07:06

I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?

相关标签:
14条回答
  • 2020-12-02 07:45

    You can try this UITextfield subclass which you can set a condition for the text to dynamically change the UIReturnKey:
    https://github.com/codeinteractiveapps/OBReturnKeyTextField

    0 讨论(0)
  • 2020-12-02 07:46

    Your UITextFields should have a delegate object (UITextFieldDelegate). Use the following code in your delegate to make the keyboard disappear:

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

    Should work so far...

    0 讨论(0)
  • 2020-12-02 07:50

    After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.

    .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
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-02 07:53

    Took me couple trials, had same issue, this worked for me:

    Check your spelling at -

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

    I corrected mine at textField instead of textfield, capitalise "F"... and bingo!! it worked..

    0 讨论(0)
  • 2020-12-02 07:53

    Swift 2 :

    this is what is did to do every thing !

    close keyboard with Done button or Touch outSide ,Next for go to next input.

    First Change TextFiled Return Key To Next in StoryBoard.

    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)
    }
    
    0 讨论(0)
  • 2020-12-02 07:53

    If you want to disappear keyboard when writing on alert box textfileds

    [[alertController.textFields objectAtIndex:1] resignFirstResponder];
    
    0 讨论(0)
提交回复
热议问题