如何使用背景点击隐藏数字键盘?
我使用这本书学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您可以通过实现这样的方法来简化此操作:
在界面生成器中使用背景视图
UIControl
而不是UIView
并将touches up事件挂接到此方法。看看这个小示例项目。我已经在第二个视图控制器中完成了这一点,其中触摸背景会关闭键盘。您还可以看到我如何将背景更改为
secondViewController.xib
中的UIControl
I think you might make this simpler by just implementing a method like this:
make the background view
UIControl
instead ofUIView
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 aUIControl
insecondViewController.xib
在不更改背景视图的情况下,您可能想要尝试的是:
Something you might want to try, without changing the background view, is:
您可能错误地完成了步骤 4(“将事件列表中的 TouchDown 方法与文件所有者连接并检查方法 backgroundTap”)。在 touchBackground 方法中包含 NSLog 以检查该方法是否被调用。
如果消息(“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.
If the message ("touchBackground was called") doesn't show up in your console, then you known touchBackground is not being called.