【发布时间】:2015-11-22 12:51:00
【问题描述】:
我想检测 UITextField 上的触摸动作。
触摸文本字段内部似乎不会触发“Touch Up Inside”动作。
【问题讨论】:
标签: ios swift uitextfield touch-up-inside
我想检测 UITextField 上的触摸动作。
触摸文本字段内部似乎不会触发“Touch Up Inside”动作。
【问题讨论】:
标签: ios swift uitextfield touch-up-inside
将 UITextField 委托设置为您的视图控制器
Obj-C
textField.delegate = self;
斯威夫特
textField.delegate = self
实现委托方法
对象-c
-(void)textField:(UITextField*)textField didBeginEditing {
// User touched textField
}
斯威夫特
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
}
【讨论】:
这里是 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”见右侧,你会发现它是可接受的控制事件
【讨论】:
myTextField.addTarget(self, action: "myTargetFunction:", forControlEvents: UIControlEvents.EditingChanged) 进行编辑,而不需要 UITextFieldDelegate
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")
}
【讨论】:
为了让这一点更清楚一点,这些东西需要到位。我用它来做到这一点,所以如果用户在我自己的信用 textField 的应用程序中输入了某些内容,则借方 textField 中的任何内容都会被删除。
put debit.addTarget 和 credit.addtarget会出现里面的视图。
@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)
....
}
}
【讨论】:
Swift 3.0 版本:
textFieldClientName.addTarget(self, action: Selector(("myTargetFunction:")), for: UIControlEvents.touchDown)
【讨论】:
Swift 3 更新
这是 Swift 3 的代码:
myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
这是函数:
func myTargetFunction() {
print("It works!")
}
【讨论】:
对于 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!")
}
【讨论】:
我参考了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
}
}
【讨论】:
class ViewController: UIViewController, UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == myTextField {
print("You edit myTextField")
}
}
}
这是一个委托函数。
【讨论】:
斯威夫特 4.2。
尝试 .editingDidEnd 而不是 .touchDown 和 delegate。
myTextField.addTarget(self, action: #selector(myTargetFunction), for: .editingDidEnd)
@objc func myTargetFunction(textField: UITextField) {
print("textfield pressed")
}
【讨论】:
在 xamarin.iOS 上我易于使用:
YourTextField.WeakDelegate = this;
[...]
[Export("textFieldDidBeginEditing:")]
public void TextViewChanged(UITextField textField)
{
if (YourTextField.Equals(textField))
IsYourTextFileFocused = true;
}
【讨论】: