0

The following code causes a crash every time I save a date using the date-picker. The new date is saved, but not before XCode terminating. There's something wrong with my "DatePickerValueChanged" Please tell me why this isn't working:

Code:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    //hide blank cells in table view
    let backgroundView = UIView(frame: CGRectZero)
    tableView.tableFooterView = backgroundView


    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewTask")
    self.navigationItem.rightBarButtonItem = addButton

    setupUIElements()

    datePicker.addTarget(self, action: "datePickerValueChanged", forControlEvents: UIControlEvents.ValueChanged)


}

Function:

@IBOutlet weak var datePicker: UIDatePicker!

@IBAction func datePickerValueChanged(sender: UIDatePicker) {
saveTHeCourseWork("dueDate", value: sender.date) // selected date value is     saved as the value for "dueDate"
}

Error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Program4.DetailViewController datePickerValueChanged]: unrecognized selector sent to instance 0x7fce737baa40
  • 3
    This is certainly a duplicate. You need to add a colon: "datePickerValueChanged:" to your selector string. – vacawama May 14 '16 at 16:20
  • Do I need to add it to the function as well? How would it look like? – IOS Programmer May 14 '16 at 16:21
  • 1
    @IOSProgrammer The colon is used to denote an argument to the method. As `datePickerValueChanged` takes an argument, you need a single colon suffix in the selector name `"datePickerValueChanged:"`. You don't need to change the method name itself. – Hamish May 14 '16 at 16:24
  • So: datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged) ? – IOS Programmer May 14 '16 at 16:26
  • @IOSProgrammer Sure... feel free to test it yourself! – Hamish May 14 '16 at 16:28

0 Answers0