我目前有4个UITextField
@IBOutlet weak var fNameTextField: UITextField!
@IBOutlet weak var lNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var phoneTextField: UITextField!
我希望你能翻译这段话:“我想跟踪他们的各种事件:”
[UIControlEvents.EditingChanged, UIControlEvents.EditingDidBegin, UIControlEvents.EditingDidEnd ]
但是我不想有3个单独的事件处理程序,所以我创建了一个像这样的单个函数。这个函数很好地告诉我哪个UITextField触发了事件,但它没有告诉我触发了哪个事件。
fNameTextField.addTarget(self, action: "onChangeTextField:", forControlEvents: UIControlEvents.AllTouchEvents)
lNameTextField.addTarget(self, action: "onChangeTextField:", forControlEvents: UIControlEvents.AllTouchEvents)
emailTextField.addTarget(self, action: "onChangeTextField:", forControlEvents: UIControlEvents.AllTouchEvents)
phoneTextField.addTarget(self, action: "onChangeTextField:", forControlEvents: UIControlEvents.AllTouchEvents)
func onChangeTextField(sender:UITextField){
switch(sender){
case fNameTextField:
print("First Name")
case lNameTextField:
print("Last Name")
case emailTextField:
print("E-mail")
case phoneTextField:
print("Phone")
default: break
}
}
如何同时打印发送者的名称和触发的事件名称(例如:.EditingDidEnd,.EditingDidEnd,.EditingDidEnd)?
理想情况下,我不希望编写多个事件处理程序,我更喜欢一个单一函数。
类似于这样:
func onChangeTextField(sender:UITextField){
switch(sender.eventTriggerd){
case UIControlEvents.EditingChanged:
println("EditingChanged")
case UIControlEvents.EditingDidBegin:
println("EditingDidBegin")
case UIControlEvents.EditingDidEnd:
println("EditingDidEnd")
default: break
}
}