How to hide Textbox Keyboard when “Done / Return” Button is pressed Xcode 4.2

爱⌒轻易说出口 提交于 2019-12-03 01:50:38

问题


I created a Text Field in Interface Builder. I set it's "Return Key" to Done. This is a one line only input (so it wont need multiple lines).

How do I hide the virtual keyboard when the user taps the done button?


回答1:


Implement the delegate method UITextFieldDelegate, then:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.yourIBtextField.delegate = self;
}

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



回答2:


UITextView does not have any methods which will be called when the user hits the return key.

Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.

There might be other ways but I am not aware of any.

Make sure you declare support for the UITextViewDelegate protocol.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

I hope it will helps a lot.




回答3:


Make category on UIViewController with next method

- (void)hideKeyboard
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                               to:nil
                                             from:nil
                                         forEvent:nil];
}


来源:https://stackoverflow.com/questions/10965357/how-to-hide-textbox-keyboard-when-done-return-button-is-pressed-xcode-4-2

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