iPad 'dismiss keyboard' button doesn't dismiss keyboard

笑着哭i 提交于 2019-12-18 09:48:35

问题


I have a UITextField in a Landscape view, and when I press the 'dismiss keyboard' button in the lower right of the UIKeyboard view, the keyboard does NOT disappear. Is there a way to programmatically listen for when this key was pressed? Or is there a connection I am not seeing that will make this keyboard go away? This is iOS 4 and XCode 4. Thanks.


回答1:


I had same problem today and I wondered, wy it works in Apple's KeyboardAccessory Sample Code. So I did reverse engineering. The ViewController was not the mistake I made in my case.

In the implementation of UIApplicationDelegate there is the entry point of application, where root viewcontroller and window will be setup - (void) applicationDidFinishLaunching:(UIApplication *)application. If you forgot to add root viewcontrollers view to window as subview, the dismiss-keyboard-button wouldn't work in any view of your app.

@class ViewController;

@interface KeyboardAccessoryAppDelegate : NSObject <UIApplicationDelegate> {
  UIWindow *window;
  ViewController *viewController;
}

  @property (nonatomic, retain) IBOutlet UIWindow *window;
  @property (nonatomic, retain) IBOutlet ViewController *viewController;

@end

...

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

  [window addSubview:viewController.view];
  [window makeKeyAndVisible];
}

Please don't forget to setup the outlets in the main xib file.

I dont know why this is related to keyboards behavior. But my theory is, that the responder chain is not linked to window, but it needs.




回答2:


To dismiss the keyboard using the dismiss keyboard button you need to implement the delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //Check if your text field is first responder, and if it is, have it resign
}

Alternatively, if you want to dismiss the keyboard by tapping outside of it, use

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UIView* view in self.view.subviews)
    {
        if ([view isKindOfClass:[UITextField class]]) {
            [view resignFirstResponder];
        }

        if ([view isKindOfClass:[UITextView class]]) {
            [view resignFirstResponder];
        }
    }
}



回答3:


you need to tell the text field that is accepting the keyboard input to no longer be the first responder.

[UITextField resignFirstRepsonder];


来源:https://stackoverflow.com/questions/5888735/ipad-dismiss-keyboard-button-doesnt-dismiss-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!