【问题标题】:How to pass arguments for a UIGestureRecognizer action如何为 UIGestureRecognizer 操作传递参数
【发布时间】:2014-03-04 10:32:09
【问题描述】:

我想检测我的UITextField 是否被触摸。我正在使用它来检测触摸:

[self.myTextField addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldTapped)]];

我有一个方法

- (void)textFieldTapped:(UITextField *)textField {
    NSLog(@"is Tapped");
}

我想使用@selector(textFieldTapped)UITextField 添加为参数,但出现错误。最好的方法是什么?

【问题讨论】:

  • UITextField 委托方法已经可用。你为什么用UITapGestureRecognizer
  • 查看我的回答 @user2951348 以获得完整解释。

标签: ios iphone textfield


【解决方案1】:
[self.myTextField addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldTapped:)]];

您错过了选择器名称末尾的冒号。

编辑:动作应该是这样的

-(void)textFieldTapped : (UIGestureRecognizer*)rec{
  NSLog(@"is Tapped");
}

【讨论】:

  • 这是答案,但是传递给textFieldTapped:的参数不是UITextField 而是UIGestureRecognizer
  • 如何获取 textField 是触摸的?
  • UITextField* textField = (UITextField*)rec.view;
  • 查看我的回答 @user2951348 以获得完整解释。
【解决方案2】:

目标操作选择器可以采用 1 个参数来指示此消息的发送者。您可以添加一个冒号来发送消息,但不能获取UITextField 作为参数,而只能获取发送者(这里是UITapGestureRecognizer)作为参数。应该是这样的:

[self.myTextField addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldTapped:)]];

-(void)textFieldTapped : (UITapGestureRecognizer *)gr{
    NSLog(@"is Tapped");
}

如果您想了解有关文本字段的一些详细信息,可以使用 UITapGestureRecognizer 的 view 属性

UITextField *tf = (UITextField *)gr.view

【讨论】:

    【解决方案3】:

    你只需要做一点修改

    [self.myTextField addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldTapped:)]];
    

    方法是

    -(void)textFieldTapped : (UITapGestureRecognizer *)recognizer
    {
        UITextField *tappedTextField=(UITextField *)[[recognizer self] view]; // this is the textfield that is being tapped.
        //You can access all the property associated with the textField.
    }
    

    【讨论】:

      【解决方案4】:

      虽然您收到的错误是由于在textFieldTapped 之后没有在@selector(textFieldTapped) 中包含冒号,但即使有冒号,您的代码也不会按预期工作。这是因为UIGestureRecognizer 操作方法只接受UIGestureRecognizer 作为参数;如果您将UIGestureRecognizer 的目标设置为@selector(textFieldTapped:),则只要方法指示它接受它作为参数,UIGestureRecognizer 本身就会自动传递。前任。 (在UITapGestureRecognizer的情况下):

      - (void)textFieldTapped:(UITapGestureRecognizer*)gesture {
          NSLog(@"is Tapped");
      }
      

      然后要找出哪个UITextField被触摸了,您可以使用该textFieldTapped:方法获取UITextField对象:

      UITextField *textField = (UITextField*)gesture.view;
      

      但在这种特殊情况下,根本没有必要创建手势识别器,因为已经有一个适当的UITextField 委托方法来检测UITextField 中的触摸。将UITextFieldDelegate 添加到.h 并设置theParticularTextField.self = delegate 后,我建议使用:

      // Called whenever a text field is selected as long as its delegate is set to self
      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
      
          if (textField == theParticularTextField) {
              // Do whatever you want to do when that particular text field is tapped.
          }
      
          return YES;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        相关资源
        最近更新 更多