0

I have a UIPickerView and I am adding a UIToolBar with UIBarButtonItems. When I press button «Done», Do not call the function «donePress». How to call it?

class SearchViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {


    @IBOutlet weak var Picker: UIPickerView! = UIPickerView()

******************************

override func viewDidLoad() {
       super.viewDidLoad()
       Picker.delegate = self
       Picker.dataSource = self
       self.setToolBar()
}

*******************************

func setToolBar() {
            let toolbar : UIToolbar = UIToolbar()
            toolbar.tag = 10
            toolbar.sizeToFit()
            toolbar.barStyle = UIBarStyle.BlackTranslucent
            toolbar.tintColor = UIColor.orangeColor()
            toolbar.translucent = true


            let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePress")
            let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
            let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "donePress")

            toolbar.setItems([cancelButton, spaceButton, doneButton], animated: false)
            toolbar.userInteractionEnabled = true            
            self.Picker.addSubview(toolbar)

}

**********************************

       func donePress() {
        print(«say Hello!»)
    }
Anci
  • 84
  • 8
  • Possible duplicate of [How to set the action for a UIBarButtonItem in Swift](http://stackoverflow.com/questions/24641350/how-to-set-the-action-for-a-uibarbuttonitem-in-swift) – mech Jan 12 '16 at 20:49
  • I try it. Don't work. – Anci Jan 13 '16 at 09:19

1 Answers1

0

Better late then never. Try replacing action: "donePress" with action: #selector(self.donePress). So it becomes:

let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.donePress))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, #selector(self.donePress))
Sem
  • 21
  • 5