【问题标题】:How to hide number pad using background Tap?如何使用背景点击隐藏数字键盘?
【发布时间】:2011-12-24 18:36:00
【问题描述】:

我使用this book 学习 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。

这是我的代码示例

附:对不起我的英语,我把书中的所有信息都翻译成英文,因为我有俄语翻译。

【问题讨论】:

    标签: ios xcode numpad


    【解决方案1】:

    我认为你可以通过实现这样的方法来简化它:

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

    在界面生成器中创建背景视图UIControl 而不是UIView,并将修饰事件与此方法挂钩。

    看看这个small example project。我在secondViewController 中做到了这一点,触摸背景会关闭键盘。您还可以看到我如何在secondViewController.xib 中将背景更改为UIControl

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-30
          • 1970-01-01
          • 2011-12-23
          • 2023-03-22
          • 2012-06-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多