ios - 无法识别的选择器发送到实例 Swift 2.1 - 日期选择器

标签 ios swift uidatepicker unrecognized-selector

每次我使用日期选择器保存日期时,以下代码都会导致崩溃。新日期已保存,但不会在 XCode 终止之前保存。我的“DatePickerValueChanged”有问题请告诉我为什么这不起作用:

代码:

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)


}

功能:

@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"
}

错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Program4.DetailViewController datePickerValueChanged]: unrecognized selector sent to instance 0x7fce737baa40

最佳答案

您的函数有一个参数。这意味着它需要在函数中发送一些东西。这是您的发件人。当您添加目标时,您需要确保您的发件人被发送到该功能。为此,只需在选择器中添加一个分号即可。

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

看看我是如何在 datePickerValueChanged 之后添加分号的。那就是将 datePicker 发送到函数。这link可能会帮助您理解这一点。

关于ios - 无法识别的选择器发送到实例 Swift 2.1 - 日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218737/

相关文章:

ios - 如何从模型中的函数接收 View 中的异步结果?

swift - Xcode无缘无故创建断点

ios - 具有来自一个 NSDate 的日期组件和来自另一个 NSDate 的时间组件的 NSDate

ios - 以编程方式创建的 UIControl 中的自动布局似乎不起作用

ios - 在 iOS swift 中使用 datePicker 处理多个文本字段

objective-c - 我如何为 Hijri 自定义 UIDatePicker

ios - 在 TableView Controller 中的 TableView 上添加模糊 View

ios - 健康工具包观察者查询总是在应用程序激活时调用

ios - Xcode 8.1 目标包含使用旧版本的 swift 开发的源代码

ios - 如何根据其中包含的某些值过滤数组