简体   繁体   English

出现键盘时,如何使键盘以外的其他接口不可用?

[英]How to make other interfaces than keyboard unavailable when keyboard appears?

I learned how to dismiss keyboard when the return key is clicked, or when user touches outside of textfield. 我了解了如何在单击返回键或用户触摸文本框之外时关闭键盘。

One way is to delegate a class to manage textfield and write the code: 一种方法是委托一个类来管理文本字段并编写代码:

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; //I'm not sure whether I choose YES or NO
}

Another way is like following: 另一种方法如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touchToDismissKeyboard.phase == UITouchPhaseBegan) {
    [self.firstTextField resignFirstResponder];
    }
}

However, with either of the methods above, I found other interfaces are "enabled", such as segmented control or switch which are on the same view as textfield. 但是,无论使用上述哪种方法,我都发现“启用”了其他接口,例如与文本字段在同一视图上的分段控件或开关。 In addition, when these enabled interfaces is clicked, the keyboard is still on the screen. 此外,单击这些启用的界面时,键盘仍在屏幕上。

Then, my question are how I can make other interfaces unavailable ("enabled = NO") when keyboard appears, and how I can dismiss keyboard with touch on anywhere out of keyboard or textfield without changing values of other interfaces. 然后,我的问题是,当键盘出现时如何使其他界面不可用(“启用=否”),以及如何在不更改其他界面值的情况下通过触摸键盘或文本字段中的任何位置来关闭键盘。

Use Tap gesture recognizer 使用点击手势识别器

UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmissKeyBoard)];
tapRecognize.delegate = self;
tapRecognize.numberOfTapsRequired = 1;
[self.viewB addGestureRecognizer:tapRecognize];

Then dissmissKeyBoard method implementation. 然后dissmissKeyBoard方法实现。

- (void)dissmissKeyBoard
{
    [yourTextField resignFirstResponder];
}

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM