【问题标题】:iOS/Swift: how to detect touch action on a UITextFieldiOS/Swift:如何检测 UITextField 上的触摸动作
【发布时间】:2015-11-22 12:51:00
【问题描述】:

我想检测 UITextField 上的触摸动作。

触摸文本字段内部似乎不会触发“Touch Up Inside”动作。

【问题讨论】:

    标签: ios swift uitextfield touch-up-inside


    【解决方案1】:

    将 UITextField 委托设置为您的视图控制器

    Obj-C

    textField.delegate = self;
    

    斯威夫特

    textField.delegate = self
    

    实现委托方法

    对象-c

    -(void)textField:(UITextField*)textField didBeginEditing {
       // User touched textField
    }
    

    斯威夫特

    func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method
    
    }
    

    【讨论】:

      【解决方案2】:

      这里是 Swfit:

      您不需要使用“touchUpInside”,只需使用委托方法,如下所示:

      让您的视图控制器成为委托:

      class ViewController: UIViewController, UITextFieldDelegate{
      
      func textFieldDidBeginEditing(textField: UITextField) -> Bool {
          if textField == myTextField {
              return true // myTextField was touched
          }
      }
      

      这是其他委托方法:

      protocol UITextFieldDelegate : NSObjectProtocol {
      
          optional func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
          optional func textFieldDidBeginEditing(textField: UITextField) // became first responder
          optional func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
          optional func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
      
          optional func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text
      
          optional func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications)
          optional func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
      }
      

      来自 swift 文档:

      struct UIControlEvents : RawOptionSetType {
          init(_ rawValue: UInt)
          init(rawValue: UInt)
      
          static var TouchDown: UIControlEvents { get } // on all touch downs
          static var TouchDownRepeat: UIControlEvents { get } // on multiple touchdowns (tap count > 1)
          static var TouchDragInside: UIControlEvents { get }
          static var TouchDragOutside: UIControlEvents { get }
          static var TouchDragEnter: UIControlEvents { get }
          static var TouchDragExit: UIControlEvents { get }
          static var TouchUpInside: UIControlEvents { get }
          static var TouchUpOutside: UIControlEvents { get }
          static var TouchCancel: UIControlEvents { get }
      
          static var ValueChanged: UIControlEvents { get } // sliders, etc.
      
          static var EditingDidBegin: UIControlEvents { get } // UITextField
          static var EditingChanged: UIControlEvents { get }
          static var EditingDidEnd: UIControlEvents { get }
          static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing
      
          static var AllTouchEvents: UIControlEvents { get } // for touch events
          static var AllEditingEvents: UIControlEvents { get } // for UITextField
          static var ApplicationReserved: UIControlEvents { get } // range available for application use
          static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use
          static var AllEvents: UIControlEvents { get }
      }
      

      UITextField 不响应“touchUpInside”见右侧,你会发现它是可接受的控制事件

      【讨论】:

      • 感谢@Lacerax,但我想检测然后用户点击文本字段。我已经检测到使用此功能myTextField.addTarget(self, action: "myTargetFunction:", forControlEvents: UIControlEvents.EditingChanged) 进行编辑,而不需要 UITextFieldDelegate
      • 所以你的意思是触摸而不是输入文本字段,对吧?
      • 你应该阅读 UITextField 上的文档,它只响应这些选择器方法: UIControlEventEditingDidBegin = 1
      • touchupinside 不是其中之一
      • 一口气看答案
      【解决方案3】:

      UITextField 似乎没有启用“Touch Up Inside”,但“Touch Down”有效。

      所以解决方法如下:

      Swift 4.x

      myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
      @objc func myTargetFunction(textField: UITextField) {
          print("myTargetFunction")
      }
      

      Swift 3.x

      myTextField.addTarget(self, action: #selector(myTargetFunction), for: UIControlEvents.touchDown)
      
      @objc func myTargetFunction(textField: UITextField) {
          print("myTargetFunction")
      }
      

      【讨论】:

      • 有趣的解决方案,但不适用于 Swift 4。
      • 为我工作,4.1 swift
      • 我正在尝试在文本字段上调用 ​​web 服务点击键盘来了,webservice 从未调用过。但是当我使用editingBegin 属性时调用了Web 服务,它进入了无限循环。谁能告诉我如何使文本字段不可编辑,
      • shouldBeginEditing 是用于此目的的最佳委托方法。
      • 只要文本字段不在滚动视图中,它就可以工作
      【解决方案4】:

      为了让这一点更清楚一点,这些东西需要到位。我用它来做到这一点,所以如果用户在我自己的信用 textField 的应用程序中输入了某些内容,则借方 textField 中的任何内容都会被删除。

      1. UITextFieldDelegate 需要在视图控制器中声明,即 class SecondViewController:
      2. 检测器函数 func myDebitDetector func myCreditDetector 需要在 ViewController 类中。
      3. put debit.addTarget 和 credit.addtarget会出现里面的视图。

      4. @IBOutlet weak var debit: UITextField!@IBOutlet weak var credit: UITextField! 是文本字段在情节提要上并连接到 viewController。

        class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
        
        @IBOutlet weak var credit: UITextField!
        @IBOutlet weak var debit: UITextField!
        
            func myDebitDetector(textfield: UITextField ){
                print("using debit")
                credit.text = ""
        
            }
        
            func myCreditDetector(textfield: UITextField) {
                print("using cedit")
                debit.text = ""
            }
        
            override func viewWillAppear(animated: Bool) {
        
        
        
                debit.addTarget(self, action: "myDebitDetector:", forControlEvents: UIControlEvents.TouchDown)
                credit.addTarget(self, action: "myCreditDetector:", forControlEvents: UIControlEvents.TouchDown)
            ....
        
            }
        }
        

      【讨论】:

        【解决方案5】:

        Swift 3.0 版本:

        textFieldClientName.addTarget(self, action: Selector(("myTargetFunction:")), for: UIControlEvents.touchDown)
        

        【讨论】:

        • 这不起作用...我得到:“UITextView”类型的值没有成员“addTarget”
        【解决方案6】:

        Swift 3 更新

        这是 Swift 3 的代码:

        myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
        

        这是函数:

        func myTargetFunction() {
            print("It works!")
        }
        

        【讨论】:

          【解决方案7】:

          对于 Swift 3.1:

          1) 创建手势识别器:

          let textViewRecognizer = UITapGestureRecognizer()
          

          2) 向识别器添加处理程序:

          textViewRecognizer.addTarget(self, action: #selector(tappedTextView(_:))) 
          

          3) 将识别器添加到您的文本视图中:

          textView.addGestureRecognizer(textViewRecognizer)
          

          4) 将处理程序添加到您的类中:

          func tappedTextView(_ sender: UITapGestureRecognizer) {
                  print("detected tap!")
          }
          

          【讨论】:

            【解决方案8】:

            我参考了UIControlEvents documentation from Apple 并想出了以下内容:

            首先将 UITextFieldDelegate 添加到你的类中,

            textBox.delegate = self
            textBox.addTarget(self, action: #selector(TextBoxOn(_:)),for: .editingDidBegin)
            textBox.addTarget(self, action: #selector(TextBoxOff(_:)),for: .editingDidEnd)
            

            具有以下功能:

            func TextBoxOff(_ textField: UITextField) {
                 code
                     }                
            }
            
            func TextBox(_ textField: UITextField) {
                code
                     }
            }
            

            【讨论】:

            • 除了你的答案之外的所有答案对我来说都没用!只有它完美地工作!谢谢:)
            • 真棒答案,完美地为我工作,谢谢:)
            【解决方案9】:

            Swift 4 及更高版本:

            class ViewController: UIViewController, UITextFieldDelegate {
                func textFieldDidBeginEditing(_ textField: UITextField) {
                    if textField == myTextField {
                        print("You edit myTextField")
                    }
                }
            }
            

            这是一个委托函数。

            【讨论】:

            • @Gowthaman 您可能忘记将文本字段委托设置为 self(ViewController)
            【解决方案10】:

            斯威夫特 4.2。

            尝试 .editingDidEnd 而不是 .touchDowndelegate

            myTextField.addTarget(self, action: #selector(myTargetFunction), for: .editingDidEnd)
            
            @objc func myTargetFunction(textField: UITextField) {
                print("textfield pressed")
            }
            

            【讨论】:

              【解决方案11】:

              在 xamarin.iOS 上我易于使用:

              YourTextField.WeakDelegate = this;
              

              [...]

              [Export("textFieldDidBeginEditing:")]
              public void TextViewChanged(UITextField textField)
              {
                  if (YourTextField.Equals(textField))
                      IsYourTextFileFocused = true;
              }
              

              【讨论】:

                猜你喜欢
                • 2011-10-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-10-19
                • 2011-06-11
                • 1970-01-01
                相关资源
                最近更新 更多