Sachin Biradarpatil
Sachin Biradarpatil

Reputation: 173

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) 

Upvotes: 0

Views: 1256

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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

}

Upvotes: 1

Vince O'Sullivan
Vince O'Sullivan

Reputation: 2701

You appear to have cut and pasted an answer from this question (Add buttons to UIPickerView - Swift 1.2) into your application without understanding how it works. That is not a good way to learn how to write code.

The error message is telling you that you are trying to call a selector (or function or action) called "donePicker" but that no such selector exists. You need to create an action in the class called "donePicker" (and also one called "canclePicker" (sic.)) for your code to work.

Upvotes: 0

Related Questions