UIKeyboardWillHide not triggered

前端 未结 5 1296
深忆病人
深忆病人 2021-01-20 12:43

I read many post here about this topic, but I wasn\'t able to find an answer to my question, so, hope you won\'t be bored about another UIKeyboard post :-)

In my vi

相关标签:
5条回答
  • 2021-01-20 12:46

    To set the keyboard up so that it has a "Done" button, do this:

    1) Setup your view controller so that it implements the UITextFieldDelegate. For Example:

    #import <UIKit/UIKit.h>
    
    @interface TX_ViewController : UIViewController <UITextFieldDelegate>
    
    @property (nonatomic, retain) IBOutlet UITextField *textField;
    
    @end
    

    2) In your view controllers implementation file, use the following code to setup the keyboard:

    - (void)viewDidLoad
    {
        [self.textField setDelegate:self];
        [self.textField setReturnKeyType:UIReturnKeyDone];
        [self.textField addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    3) And if you wish to do something when the DONE button is pressed, simply add the following function to your view controller's implementation file:

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

    Also, if you are using Interface builder to create your interfaces, don't forget to setup your IBOutlet reference for the TextField; otherwise, your class won't receive the messages from the XIB.

    I set this up in a sample application just to see if it works and it did perform in the way you wish for your application to perform.

    0 讨论(0)
  • 2021-01-20 12:48

    It's important to note that when the user hides the software keyboard via the hide button, the hide methods aren't called. The show methods are called again, but the keyboard is nearly off screen except for the home row toolbar.

    0 讨论(0)
  • 2021-01-20 13:02

    If you read the documents for UIWindow it says that the notification object for these notifications is nil. You are passing self.view.window in as the object to the addObserver:selector:name:object: method. Try passing nil instead:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    0 讨论(0)
  • 2021-01-20 13:02

    Check, if keyboardDone really gets called (i.e. with NSLog(@"%@", @"keyboard done called");). If its get called, but resignFirstResponder does not help dismissing the keyboard, then try this:

    [self.view endEditing:YES];

    Please also provide your keyboardWillHide: method.

    0 讨论(0)
  • 2021-01-20 13:03

    Swift $

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    
    func keyboardWillHide(notification: NSNotification){
         print("keyboardWillHide")
    }
    
    0 讨论(0)
提交回复
热议问题