0

I have VC in my app (not root VC), and I wont to add boxes there, after clicking button. Adding work fine. My var is in another model swift file (av = ActualValues()). Default var is 0, and every time when I dismiss my VC, var back to value 0. How can I fix it? UserDefaults doesn't work :/ That's my code Actual Values (look at var quantity):

import Foundation
import Firebase


class ActualValues {

var id = "not changed"
var databaseID = "not changed"
var shift = 1
var product: String?
var target: Int = 0
var issueNumber: String?
var issue: String?
var issueNote: String?
var breakNumber: String?

var customer: String?
var quantity: Int = 0
var teamLeader: String?
var numberOfPeople: String?
var numberOfRejects = "0"
var reasonOfRejects: String?
var endNote: String?
var podType: String = "Not selected"
var typeOfWork = "not specified"
...

And statsVC:

import UIKit

class CurrentStatsViewController: UIViewController {

var av = ActualValues()
var defaultsQuantity = UserDefaults.standard.integer(forKey: "quantity")

@IBOutlet weak var menuButton: MyButton!
@IBOutlet weak var boxButton: MyButton!
@IBOutlet weak var rejectButton: MyButton!

//Labels
@IBOutlet weak var planLabel: UILabel!
@IBOutlet weak var actualLabel: UILabel!
@IBOutlet weak var rejectsLabel: UILabel!
@IBOutlet weak var effLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    menuButton.isEnabled = true
    boxButton.isEnabled = true
    rejectButton.isEnabled = true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    planLabel.text = "\(av.target)"
    actualLabel.text = "\(av.quantity)"
}

@IBAction func menuButtonTapped(_ sender: MyButton) {
    self.dismiss(animated: true, completion: nil)
}

@IBAction func boxButtonTapped(_ sender: MyButton) {
    addQuantity()
}

@IBAction func rejectsButtonTapped(_ sender: MyButton) {

}

//FUNC
func addQuantity() {
    av.quantity = (av.quantity + 1)
    actualLabel.text = "\(av.quantity)"
    print(av.quantity)
}
}

How can I save var every time when I dismiss segue, and load last number if I back to this VC?

  • Can't you just set your ActualValues property in viewWillDisappear to 0? Or maybe rather viewDidDisappear https://developer.apple.com/documentation/uikit/uiviewcontroller/1621485-viewwilldisappear – Yanchi May 28 '20 at 10:19
  • No. For example I'va added 10 boxes. Then I dismiss VC, and do sometging else in app. After 2 min back to VC and want to add 5 more boxed. So when I back, I want to see 10 in my Label, and add 5. And when I back next time I want to see 15, and add more... But now every time when I open VC, av.quantity and label are 0. – Michael_Gruszka May 28 '20 at 10:32
  • 1
    Ah ok I see. Well you have 2 options. You either save it in nsuserdefaults as you tried in viewWillDisappear (example - https://stackoverflow.com/questions/31203241/how-can-i-use-userdefaults-in-swift) or you save it in your model. The problem is, the model needs to be persistent, where are your creating it? – Yanchi May 28 '20 at 10:38
  • 1
    Create a static variable and assign the variable value to it.. and after that you can access the value from any place.. Or you can use singleton pattern – Nicol Vishan May 28 '20 at 11:52

2 Answers2

1

You can use UserDefaults to save and load the data. Here's an example for an Int:

UserDefaults.standard.set(3, forKey: "myKeyName")

To load the value use a similar method. However, note the type is specified in the function call.

UserDefaults.standard.integer(forKey: "myKeyName")

I would also recommend using a struct to store the keys as this provides an extra layer of type safety.

struct UserDefaultKey {
    static let myInt = "myKeyName"
}

If you are using segues, you can use the following method to handle any data before the segue takes place:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    // Save your data here
    UserDefaults.standard.set(3, forKey: UserDefaultKey.myInt)
}
1

I would also consider using a singleton class.