I implemented core data and UISteppers in some fields. Every time I am trying to edit a record the UIStepper start counting from 1.
import CoreData
class AktieViewController: UIViewController
{
@IBOutlet weak var titleTF: UITextField!
@IBOutlet weak var typeTF: UITextField!
@IBOutlet weak var notesTV: UITextView!
@IBOutlet weak var counterTF: UITextField!
@IBOutlet weak var pointTF: UITextField!
@IBOutlet weak var savingsTF: UITextField!
@IBOutlet weak var completedTF: UITextField!
@IBOutlet weak var counterStepper: UIStepper!
@IBOutlet weak var pointsStepper: UIStepper!
@IBOutlet weak var savingsStepper: UIStepper!
var selectedAktie: Aktie? = nil
override func viewDidLoad()
{
super.viewDidLoad()
if(selectedAktie != nil)
{
titleTF.text = selectedAktie?.title
typeTF.text = selectedAktie?.type
notesTV.text = selectedAktie?.notes
savingsTF.text = selectedAktie?.saving
counterTF.text = selectedAktie?.counter
pointTF.text = selectedAktie?.point
completedTF.text = selectedAktie?.completed
}
}
// Mark: this is to save the action
@IBAction func saveAction(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
if(selectedAktie == nil)
{
let entity = NSEntityDescription.entity(forEntityName: "Aktie", in: context)
let newAktie = Aktie (entity: entity!, insertInto: context)
newAktie.id = aktieList.count as NSNumber
newAktie.title = titleTF.text
newAktie.type = typeTF.text
newAktie.notes = notesTV.text
newAktie.saving = savingsTF.text
newAktie.point = pointTF.text
newAktie.counter = counterTF.text
newAktie.completed = completedTF.text
do {
try context.save()
aktieList.append(newAktie)
navigationController?.popViewController(animated: true)
}
catch
{
print("context save error")
}
}
else // edit
{
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")
do {
let results: NSArray = try context.fetch(request) as NSArray
for result in results {
let aktie = result as! Aktie
if(aktie == selectedAktie)
{
aktie.title = titleTF.text
aktie.type = typeTF.text
aktie.notes = notesTV.text
aktie.saving = savingsTF.text
aktie.point = pointTF.text
aktie.counter = counterTF.text
aktie.completed = completedTF.text
try context.save()
navigationController?.popViewController(animated: true)
}
}
}
catch
{
print("Fetch Failed")
}
}
}
// this is to delete the action
@IBAction func deletedAktie(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")
do {
let results: NSArray = try context.fetch(request) as NSArray
for result in results {
let aktie = result as! Aktie
if(aktie == selectedAktie)
{
aktie.deletedDate = Date ()
try context.save()
navigationController?.popViewController(animated: true)
}
}
}
catch
{
print("Fetch Failed")
}
}
// This function adds the stepper to a field
//issue: it does not remember the score when i edit it and starts over
@IBAction func counterStepperPressed(_ sender: UIStepper) {
counterTF.text = Int(sender.value).description
}
@IBAction func pointStepperPressed(_ sender: UIStepper) {
pointTF.text = Int(sender.value).description
}
@IBAction func savingsStepperPressed(_ sender: UIStepper) {
savingsTF.text = Int(sender.value).description
}
}
Reading back your initial post :
Every time I am trying to edit a record the UIStepper start counting from 1.
@IBAction func counterStepperPressed(_ sender: UIStepper) {
counterTF.text = Int(sender.value).description
}
If I understand, you want to increment the counterTF, not just set it with the stepper value ?
So, you should simply take this initial value, something like:
@IBAction func counterStepperPressed(_ sender: UIStepper) {
let initialValue = Int(counterTF.text) ?? 0
let newValue = Int(sender.value) + initialValue
counterTF.text = "\(newValue)"
}
Note: I detailed each step, but you could have it more compact.