Search code examples
iosswiftuipickerview

donePicker: unrecognized selector sent to instance


I am adding cancel and done button on top of UIpickerView in my ios app, but I am getting error like- [Avakaash.Profile_add_update donePicker]: unrecognized selector sent to instance 0x7faf19daf1f0' and my code looks like below-

    override func viewDidLoad() {
    super.viewDidLoad()

    std_class_pickerView.tag = 0
    country_pickerview.tag = 1



    std_class_pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
    std_class_pickerView.backgroundColor = .whiteColor()
    std_class_pickerView.showsSelectionIndicator = true


    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()

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

    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    std_class_pickerView.delegate = self

    text_std_class.inputView = std_class_pickerView

    text_std_class.inputAccessoryView = toolBar
    }

and my controller class look like-

 class Profile_add_update: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
      }

error log-

     [Avakaash.Profile_add_update donePicker]: unrecognized selector sent to instance 0x7faf19daf1f0
     2016-08-18 17:04:13.653 Avakaash[2999:227329] *** Terminating app due to   uncaught exception 'NSInvalidArgumentException', reason: 
    '-      [Avakaash.Profile_add_update donePicker]: unrecognized selector 
     sent to instance 0x7faf19daf1f0'

     libc++abi.dylib: terminating with uncaught exception of type NSException
     (lldb) 

Solution

  • you forget to implement the action of donePicker and canclePicker button do like

    func donePicker () //Create an IBAction
        {
       // do something
    
      }
    
    func canclePicker () //Create an IBAction
        {
       // do something
    
      }
    

    full code

      let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
        toolBar.sizeToFit()
    
        let doneButton = UIBarButtonItem(title: "Done", style:.Plain, target: self, action: #selector(ViewController.donePicker))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style:.Plain, target: self, action: #selector(ViewController.canclePicker))
    
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.userInteractionEnabled = true
    

    action like

    func donePicker () //Create an IBAction
    {
        // do something
    
    }
    
    func canclePicker () //Create an IBAction
    {
        // do something
    
    }