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

发布于 2024-12-22 18:53:46 字数 904 浏览 0 评论 0原文

我使用这本书学习iOS SDK,在第4章中,当它告诉你时如何通过背景点击隐藏数字键盘 我有一个问题。我照作者说的做了,但什么也没发生。我怎样才能做到这一点? 书的作者说的是:

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

- (IBAction)touchBackground:(id)sender;

2)在ViewController.m中添加方法

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

3)在Interface Builder中更改View对象的类标识(据我在Xcode 4中的理解是它的Control)从UIView到UIControl

4)将事件列表中的 TouchDown 方法与文件所有者连接并检查方法 backgroundTap。

这是我的代码示例

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

I study iOS SDK using this book and 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:

1)Create new method backgroundTap in ViewController.h

- (IBAction)touchBackground:(id)sender;

2)Add method in 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

4)Connect TouchDown method in events list with File's Owner and check method 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2024-12-29 18:53:46

我认为您可以通过实现这样的方法来简化此操作:

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

在界面生成器中使用背景视图UIControl而不是UIView并将touches up事件挂接到此方法。

看看这个小示例项目。我已经在第二个视图控制器中完成了这一点,其中触摸背景会关闭键盘。您还可以看到我如何将背景更改为 secondViewController.xib 中的 UIControl

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 UIControl instead of UIView in interface builder and hook the touches up event to this method.

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

○闲身 2024-12-29 18:53:46

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

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

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [nameField resignFirstResponder];
    [numberField resignFirstResponder];
}
新雨望断虹 2024-12-29 18:53:46

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

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

如果消息(“touchBackground 被调用”)没有出现在您的控制台中,那么您就知道 touchBackground 没有被调用。

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.

-(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文