Core data 如何将UIStepper保存到CoreData(swift 3)

Core data 如何将UIStepper保存到CoreData(swift 3),core-data,swift3,uistepper,Core Data,Swift3,Uistepper,我在tapleviewcell中使用UIStepper,但我不知道如何在CoreData中保存步进值 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Step

我在tapleviewcell中使用UIStepper,但我不知道如何在CoreData中保存步进值

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell

    //stepper is your UIStepper reference from StepperTableViewCell
    cell.stepper.tag = indexPath.row
    cell.stepper.addTarget(self, action: #selector(stepperAction(sender:)), for: .valueChanged)

    return cell
}
func stepperAction(sender: UIStepper)  {
    print("Stepper \(sender.tag) clicked. Its value \(sender.value)")
}

1.在StepperTableViewCell中创建一个变量,如下所示

var callBackStepper:((_ value:Double)->())?
2.在StepperTableViewCell中创建iAction别忘了引用UI 那可能是这样的:

@IBAction func stepperTapped(sender: AnyObject) {
   callBackStepper?(sender.value)
}
在UIViewController中,在tableView中设置回调

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell

cell.callBackStepper = { value in
   print("every time called when you use UIStepper \(value)")
 }
return cell
}

它未经测试,但应该可以工作,请给出反馈

您想做什么?只需保存发送者。值?你真的需要核心数据吗?也许用户默认值也会很棒?对于核心数据,请看这里或这里,我在我的项目中使用CoreData,stepper是其中的一部分。我知道如何将UITextfield中的文本保存到CoreData,但我不知道如何将IUStepper值保存到CoreData中。无论如何,我不理解这个问题。sender.value是一个双精度或数字,不是吗?核心数据支持双重数据。以与字符串相同的方式保存它。请看这里:我的UIStepper在tableviewcell中,我不知道如何保存刚才点击的stepper,对不起,我的语言。我还没有测试它,但是如何保存CoreData中每个单元格的stepper值?我以为你知道如何在有值时持久保存该值?您总是在回调中获取值。要持久保存,请查看stackoverflow.com/questions/37958530/using-double-in-core-da‌​ta请先测试代码!我测试了它,现在我有了这个值,但我在每个单元格中都有了这个值,因为我的项目中都有tableview,所以我如何用每个单元格中的每个新stepper值更新CoreData。你能与github或作为zip共享你的项目吗?理解起来很复杂:但我有一个tableview,我在每个单元格中都有stepper,现在我可以得到步进器值,但我不知道如何更新/编辑CoreData中的旧值。