【问题标题】:Dismiss keyboard when touching outside of UITextField in iwatch在 iwatch 中触摸 UITextField 外部时关闭键盘
【发布时间】:2015-10-26 16:38:42
【问题描述】:

我知道当我想关闭键盘时我需要告诉我的 UITextField 让第一响应者辞职,但我不确定如何知道用户何时按下了out side of view

【问题讨论】:

    标签: ios objective-c iphone keyboard uitextfield


    【解决方案1】:

    您可以通过调用如下提供的触摸视图委托方法来实现。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [[event allTouches] anyObject];
    
    if (![[touch view] isKindOfClass:[UITextField class]]) {
    [self.view endEditing:YES];
    }
    [super touchesBegan:touches withEvent:event];
    }
    

    如有任何疑问,请告诉我。

    【讨论】:

      【解决方案2】:

      它将隐藏键盘。 我希望它对你有用。

      UIGestureRecognizer *tapper;
      
      
      
      - (void)viewDidLoad {
      [super viewDidLoad];
      tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
      tapper.cancelsTouchesInView = NO;
      [self.view addGestureRecognizer:tapper];
      }
      
      - (void)handleSingleTap:(UIGestureRecognizer *)sender
      {
      [UIView beginAnimations:nil context:NULL];
      [UIView commitAnimations];
      [self.view endEditing:YES];
      
      }
      

      【讨论】:

        【解决方案3】:

        你可以试试touchesEnded方法

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

        【讨论】:

          【解决方案4】:

          我希望它有帮助..

          您可以简单地将 UITapGestureRecognizer 添加到您的 self.view 中

          UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taped:)];
          [tap setNumberOfTapsRequired:1];
          [self.view addGestureRecognizer:tap];
          
          
          -(void)taped:(UITapGestureRecognizer*)gesture
          {
              //resign first responder
          }
          

          【讨论】:

          • 如果我点击其他 UI 对象或控件会发生什么?
          猜你喜欢
          • 2011-07-15
          • 1970-01-01
          • 2015-11-23
          • 1970-01-01
          • 2011-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-22
          相关资源
          最近更新 更多