0

I use UIStepper in a tapleviewcell but I don't know how to save stepper value in my 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)")
}
Alex
  • 1
  • 1
  • What do you want to do? Just save the sender.value? Do you really need core-data? Maybe UserDefaults will be great, too? For core-data have a look here https://www.raywenderlich.com/145809/getting-started-core-data-tutorial or here https://learnappdevelopment.com/ios-app-development-free/how-to-use-core-data-in-ios-10-swift-3/ or here https://medium.com/ios-geek-community/beginners-guide-to-core-data-in-swift-3-85292ef4edd#.gnbnvyows – kuzdu Mar 15 '17 at 22:11
  • I use CoreData in my project and the stepper is a part of it. I know how to save text from UITextfield to CoreData but I don't know how to save IUStepper value in the CoreData – Alex Mar 15 '17 at 22:16
  • Anyhow I don't understand the problem. sender.value is a double or a number, isn't it? Core Data supports double. Save it in the same way like the string. See here: http://stackoverflow.com/questions/37958530/using-double-in-core-data – kuzdu Mar 15 '17 at 22:36
  • My UIStepper in a tableviewcell and I don't know how can I save the stepper who just tapped, sorry about my language. – Alex Mar 15 '17 at 22:39

1 Answers1

1

1.You create a variable in your StepperTableViewCell like this

var callBackStepper:((_ value:Double)->())?

2.You create an IBACTION in your StepperTableViewCell (don't forget make an reference to your UI) That could have a look like this:

@IBAction func stepperTapped(sender: AnyObject) {
   callBackStepper?(sender.value)
}
  1. In your UIViewController where the tableView you set the callback

    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
    }
    

It's untested, but it should work, please give a feedback

kuzdu
  • 7,124
  • 1
  • 51
  • 69
  • I don't test it yet but how to save the stepper value which is for every cell in CoreData? – Alex Mar 17 '17 at 00:38
  • I thought u know how to save the value persistently when you have the value? You always get the value in the callback. To save it persistently have a look stackoverflow.com/questions/37958530/using-double-in-core-da‌​ta Please test code first! – kuzdu Mar 17 '17 at 09:51
  • I tested it, now I have the value but I have value in every cell because I have tableview in my project, so how can I update CoreData with every new stepper value which is in every cell. – Alex Mar 17 '17 at 17:06
  • can you share your project with github or as zip? – kuzdu Mar 17 '17 at 22:10
  • It's complicated to understand:( but I have a tableview and in every cell I have stepper, now I can get the stepper value but I don't know how to update/edit the old value in the CoreData. – Alex Mar 17 '17 at 22:51
  • Please use google. The web has a lot of entries about the topic. For example that: http://stackoverflow.com/questions/26345189/how-do-you-update-a-coredata-entry-that-has-already-been-saved-in-swift You have the value, update it and then make a tableView.reloadData() where the new values are used. – kuzdu Mar 17 '17 at 23:35
  • I did, but it's not working :( can you give me an example which can be used with swift 3 to update/edit stepper value in CoreData? Thank you a lot sir:) – Alex Mar 18 '17 at 10:28