xcode 如何使用背景点击隐藏数字键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8626225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:49:39  来源:igfitidea点击:

How to hide number pad using background Tap?

iosxcodenumpad

提问by Alex Lapchenko

I study iOS SDK using this bookand in 4 Chapter, when it tells you how to hide number pad with background tap I have a problem. I do that author says but nothing happened. How I can do this? What book's author says:

我使用这本书和第 4 章学习了 iOS SDK ,当它告诉您如何使用背景点击隐藏数字键盘时,我遇到了问题。我照着作者说的做了,但什么也没发生。我怎么能做到这一点?哪本书的作者说:

1)Create new method backgroundTap in ViewController.h

1)在ViewController.h中新建方法backgroundTap

- (IBAction)touchBackground:(id)sender;

2)Add method in ViewController.m

2)在ViewController.m中添加方法

-(void)touchBackground:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];}

3)Change class identity of View object in Interface Builder(as I understand in Xcode 4 its Control) from UIView to UIControl

3) 将界面生成器中 View 对象的类标识(正如我在 Xcode 4 中理解的 Control)从 UIView 更改为 UIControl

4)Connect TouchDown method in events list with File's Owner and check method backgroundTap.

4) 将事件列表中的 TouchDown 方法与 File's Owner 连接并检查方法 backgroundTap。

This is my code samples

这是我的代码示例

P.S. I'm sorry for my English, I translated all information from the book to english because I have russian translate.

PS我很抱歉我的英语,我将书中的所有信息翻译成英语,因为我有俄语翻译。

回答by Abizern

I think you might make this simpler by just implementing a method like this:

我认为您可以通过实现这样的方法来使这更简单:

- (IBAction)backgroundTouched:(id)sender {
    [self.view endEditing:YES];
}

make the background view UIControlinstead of UIViewin interface builder and hook the touches up event to this method.

制作背景视图UIControl而不是UIView在界面构建器中,并将 touches up 事件挂钩到此方法。

Have a look at this small example project. I have done exactly this in the secondViewControllerwhere touching the background dismissed the keyboard. You can also see how I have changed the background to a UIControlin secondViewController.xib

看看这个小示例项目。我已经secondViewController在触摸背景关闭键盘的地方做到了这一点。您还可以看到我如何将背景更改为UIControlinsecondViewController.xib

回答by eric.mitchell

Something you might want to try, without changing the background view, is:

在不更改背景视图的情况下,您可能想要尝试的是:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [nameField resignFirstResponder];
    [numberField resignFirstResponder];
}

回答by P. Wright

You may have done Step 4 ("Connect TouchDown method in events list with File's Owner and check method backgroundTap") incorrectly. Include an NSLog in your touchBackground method to check whether the method is being called.

您可能错误地完成了第 4 步(“将事件列表中的 TouchDown 方法与文件所有者连接并检查方法 backgroundTap”)。在您的 touchBackground 方法中包含一个 NSLog 以检查是否正在调用该方法。

-(void)touchBackground:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
NSLog(@"touchBackground was called");
}

If the message ("touchBackground was called") doesn't show up in your console, then you known touchBackground is not being called.

如果消息(“touchBackground was called”)没有显示在您的控制台中,那么您就知道没有调用 touchBackground。