UI Stepper implementation - start counting over

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

    }

    



}
Answered by Claude31 in 712548022

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.

Hint: when pasting code, use "Paste and Match Style", to avoid all the extra blank lines.

  // This function adds the stepper to a field

       Which function ?

I have managed to add the following code to remember the value in the stepper.

  counterStepper.value=value counterTF.text=String(describing: value)

I added the following code to the stepper.

@IBAction func counterStepperPressed(_ sender: UIStepper) {
  counterTF.text=String(describing: sender.value) UserDefaults.standard.setValue(sender.value, forKey: "counterStepper") NotificationCenter.default.post(Notification.init(name: Notification.Name("StepperDidChangeValue")))
}

The only issue I have is that if I edit a second item it remembers the value of the first item. Somehow it is not remembering the original value of the second item.

I don't understand your code (beyond its editing):

counterStepper.value = value
counterTF.text=String(describing: value)

What is this value ? How is it defined ? Is it sender.value ?

I added the following code to the stepper.

@IBAction func counterStepperPressed(_ sender: UIStepper) {
  counterTF.text = String(describing: sender.value)
  UserDefaults.standard.setValue(sender.value, forKey: "counterStepper")  
  NotificationCenter.default.post(Notification.init(name: Notification.Name("StepperDidChangeValue")))
}

Who is supposed to receive this notification ? WHere is the code to handle the notification ? Do you do the same for each stepper ?

Accepted Answer

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.

UI Stepper implementation - start counting over
 
 
Q