I currently have 4 UITextField's
@IBOutlet weak var fNameTextField: UITextField!
@IBOutlet weak var lNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var phoneTextField: UITextField!
and I want to keep track of their various events:
[UIControlEvents.EditingChanged, UIControlEvents.EditingDidBegin, UIControlEvents.EditingDidEnd ]
but I don't want to have 3 event separate event handlers so I created a single function like this. This function does a great job of telling me which UITextField fired an event but it doesn't tell me which event was triggered.:
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
}
}
How can I print both the name of the sender and the name of the event triggered (ex: .EditingDidEnd, .EditingDidEnd, .EditingDidEnd)?
Ideally, I do not want to write multiple event handlers, I'd prefer a single function.
Something like this:
func onChangeTextField(sender:UITextField){
switch(sender.eventTriggerd){
case UIControlEvents.EditingChanged:
println("EditingChanged")
case UIControlEvents.EditingDidBegin:
println("EditingDidBegin")
case UIControlEvents.EditingDidEnd:
println("EditingDidEnd")
default: break
}
}
static var valueChanged: UIControl.Event. A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
adjective. A swift event or process happens very quickly or without delay.
The Event class has a generic parameter T which defines the type of data that this event conveys and the EventHandler typealias declares a function that accepts this type. The rest of this class is pretty straightforward, handlers are added to an array, with each being invoked when the event is raised.
Unfortunately, you cannot distinguish what control event triggered an action handler. This has nothing to do with Swift; it's just a feature of Cocoa.
It's a strange design decision, but that's just how it is. See, for example, my book, which complains about it:
Curiously, none of the action selector parameters provide any way to learn which control event triggered the current action selector call! Thus, for example, to distinguish a Touch Up Inside control event from a Touch Up Outside control event, their corresponding target–action pairs must specify two different action handlers; if you dispatch them to the same action handler, that handler cannot discover which control event occurred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With