resignFirstResponder not hiding keyboard on textFieldShouldReturn

后端 未结 14 1692
我在风中等你
我在风中等你 2020-12-05 10:11

I have a view with a UITextField which should hide the keyboard when return is pressed.

My function is this:

- (BOOL)textFieldShouldReturn:(UITextFie         


        
相关标签:
14条回答
  • 2020-12-05 10:35

    xcode 4.5.1

    Simply click control then on the textfield drag and release on the .h file

    (control key+ drag)

    then in the pop up menu select

    connection=acton;
    name= any name;
    type=id;
    event=did end on exit;
    arguments=sender;
    

    then click connect button

    0 讨论(0)
  • 2020-12-05 10:36

    I have read so many articles about this issue, where the onscreen keyboard refuses to hide when you call resignFirstResponder, but none of the suggestions worked for me.

    I'm using XCode 5 (iOS 7) and have a iPhone screen containing a couple of controls which require the onscreen keyboard, but if the user clicks on the UIButton, then I want the keyboard to disappear.

    enter image description here

    I probably wasted one full day experimenting with resignFirstResponder and adding disablesAutomaticKeyboardDismissal functions to return NO, but nothing worked. Once the onscreen keyboard appeared, I could never get it to disappear again.

    But then I had a small brainwave (as I only have a small brain).

    Now, when the user clicks on my UIButton, I simply disable the UITextField and UITextView controls.

    - (IBAction)btnDate_Tapped:(id)sender {
        //  The user has clicked on the "Date" button.
        self.tbClientName.enabled = NO;
        self.tbComments.editable = NO;
    

    And suddenly, the app finds it has no editable text fields needing an onscreen keyboard, and it neatly slides the keyboard out of sight.

    (Relieved sigh.)

    My UIButton actually makes a popup dialog appear. When the user dismisses the popup, I re-enable these two controls, so if the user taps in one of them, the keyboard will appear again.

    -(void)popoverControllerDidDismissPopover:(UIPopoverController *) popoverController {
        //  The user has closed our popup dialog.
        //  We need to make our UITextField and UITextView editable again.
        self.tbClientName.enabled = YES;
        self.tbComments.editable = YES;
        ... etc...
    }
    

    Simple, isn't it !

    And surprisingly, this workaround even works on UIViewControllers which appear in Modal style.

    I hope this helps other XCode victims out there.

    0 讨论(0)
  • 2020-12-05 10:38

    Did you remember to implement the UITextFieldDelegate protocol?

    0 讨论(0)
  • 2020-12-05 10:42

    if you are in UIModalPresentationFormSheet just call

    - (BOOL)disablesAutomaticKeyboardDismissal
    {
        return NO;
    }
    
    0 讨论(0)
  • 2020-12-05 10:42

    Swift 3.0

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    
        if textField == addressTextField {
            textField.resignFirstResponder()
    
            return false
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-12-05 10:45

    There is this helpful method which allows you to dismiss the keyboard when presenting the Modal Dialog:

     - (BOOL)disablesAutomaticKeyboardDismissal { return NO; }
    

    This will override the default behavior of the modal dialog set by Apple and allow you dismiss the keyboard. It is in the UIViewController Class.

    I hope this helps someone!

    0 讨论(0)
提交回复
热议问题