I cant figure out which UITextField
is currently active so i can clear its text if they hit cancel on a UIBarButtonItem
. Here is my code. There is a view with a UIPickerView
,UIToolBar
and two bar button items. The cancel item has an action that will clear its text only i cant figure out how to get the correct UITextField
.
I've tried these both:
@IBAction func cancelText(sender: AnyObject) {
if self.truckYear.editing == true{
println("truckYear")
clearText(self.truckYear)
} else if self.truckMake.editing == true{
clearText(self.truckMake)
}
}
@IBAction func doneText(sender: AnyObject) {
pickerView.hidden = true
}
func clearText(textField:UITextField){
println("in clear text")
textField.text = nil
}
And:
@IBAction func cancelText(sender: AnyObject) {
if self.truckYear.isFirstResponder(){
println("truckYear")
clearText(self.truckYear)
} else if self.truckMake.isFirstResponder(){
clearText(self.truckMake)
}
}
@IBAction func doneText(sender: AnyObject) {
pickerView.hidden = true
}
func clearText(textField:UITextField){
println("in clear text")
textField.text = nil
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…