【问题标题】:UITextField's clearbutton is not working in the UITableViewCellUITextField 的清除按钮在 UITableViewCell 中不起作用
【发布时间】:2012-08-12 15:43:27
【问题描述】:

我有一个UITableView,其中包含一些单元格,我在每个单元格中添加了一个UITextField

我设置了textField.clearButtonMode = UITextFieldViewModeWhileEditing

当我编辑 textField 时,清除按钮和键盘都出来了。我在 textField 中键入一些单词,然后点击清除按钮,键盘将被隐藏,但 textField 中的文本不会被清除。

除清除按钮外,其他所有功能都很好。

【问题讨论】:

    标签: ios uitableview uitextfield


    【解决方案1】:

    我遇到了这个问题,因为我忘记了我正在使用UITapGestureRecognizer 来捕捉桌面上的点击以关闭键盘,并且它正在捕捉清除按钮上的点击,从而阻止它运行。在UITapGestureRecognizer 上添加cancelsTouchesInView=NO 以使触摸仍然生效并检查使用CGRectContainsPoint on the tapper 方法仅结束编辑和resignFirstResponder 仅当点击不在当前UITextField 的框架上时矩形。请注意,这仍然不是完全完美的,因为在自动更正上点击 X 可能超出文本字段的框架矩形,因此检查单元格的 contentView 可能会更好。

    【讨论】:

    • 和你一样,我也在使用 UITapGestureRecognizer 来捕捉桌面上的水龙头。谢谢
    • cancelsTouchesInView=NO 应该在哪里执行?在 tap 方法中?
    • 一个更完整的例子会很好;-)
    • @JohanKarlsson 这是手势识别器的属性。
    【解决方案2】:

    如果你有手势识别器,你应该这样做

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodThatYouMayCall)];
    [myTextField addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.delegate =  self;
    gestureRecognizer.cancelsTouchesInView = NO;
    

    当您单击清除按钮时,这将清除文本字段并触发“methodThatYouMayCall”,因此您也应该这样做 你的 textField.clearButtonMode 是一种 UIButton 类,所以你可以这样做

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {   
       if ([touch.view isKindOfClass:[UIButton class]]) 
       {
           return NO;
       }
       else
       {
           return YES;
       }
    }
    

    不要忘记将类标记为实现 UIGestureRecognizerDelegate 协议。 希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      我无法重现您遇到的问题,因为触摸“清除”按钮不会也不应该让第一响应者辞职。但也许您可以将您的代码与我在下面包含的最基本用例进行比较,以找出问题所在。

      此外,我建议您阅读有关 UIResponder 的文档,因为您似乎可能不小心涉足该领域。

      @implementation TextFieldTableViewController
      
      #pragma mark - Table view data source
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          // Return the number of sections.
          return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          // Return the number of rows in the section.
          return 5;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
      
          // Remove old instances of myTextField
          for (UIView *oldView in cell.contentView.subviews)
              [oldView removeFromSuperview];
      
          // Create my new text field
          UITextField *myTextField = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
          [myTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
          [myTextField setBorderStyle:UITextBorderStyleRoundedRect];
      
          // Add the TextField to the content view
          [cell.contentView addSubview:myTextField];
      
          return cell;
      }
      
      @end
      

      【讨论】:

        【解决方案4】:

        虽然最初的问题不是由此引起的,但我认为任何未来的搜索都可能需要知道:

        您添加到UITableViewCell 的视图必须作为子视图添加到其contentView 属性,否则您可能会遇到在未启用用户交互的情况下显示的视图。

        自 iOS 2.0 起,您应该将子视图添加到 contentView

        【讨论】:

          猜你喜欢
          • 2012-03-08
          • 2013-09-12
          • 1970-01-01
          • 2018-08-27
          • 1970-01-01
          • 1970-01-01
          • 2020-03-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多