Reputation: 29
I need that when the Done button on my toolbar, that is an accessory view of a textfield, make the keyboard to be dismissed.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
var toolBar = UIToolbar()
var buttonOnToolbar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "dismiss")
let arrayOfButtons = [buttonOnToolbar]
toolBar.items = arrayOfButtons
toolBar.barTintColor = UIColor.whiteColor()
textField.inputAccessoryView = toolBar
return true
}
func dismiss(sender: UIBarButtonItem) {
println("cacca")
}
The function dismiss in just a tryout to see if something happens when the button is clicked. But when I press it nothing happens. Moreover the toolbar, although I changed his color remains transparent. What is wrong?
EDIT: Solved, the UIToolbar didn't have a size.
Upvotes: 1
Views: 282
Reputation: 14865
The problem is the wrong selector:
dismiss
selects a method that takes no argument. But your method takes one argument:
func dismiss(sender: UIBarButtonItem)
So your selector must be like this:
dismiss:
E.g.
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "dismiss:")
Upvotes: 3