When playing the timer, the values ​​changed via UIStepper and saved in another controller begin with the default value of the variable, and after the first scrolling the timer starts to be displayed with the desired changed values

1 controller:

var duretionExersise = 30.0 // дефолтное значение переменной вынесено за класс контроллера @IBOutlet weak var secondsStepper: UIStepper! @IBOutlet weak var secondsLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() if NSUserDefaults.standardUserDefaults().objectForKey("duretionExersise") != nil { duretionExersise = NSUserDefaults.standardUserDefaults().objectForKey("duretionExersise") as! Double } secondsStepper.value = duretionExersise secondsStepper.minimumValue = 1 secondsStepper.maximumValue = 60 } @IBAction func secondsStepper(sender: UIStepper) { NSUserDefaults.standardUserDefaults().setObject(Int(sender.value), forKey: "duretionExersise") NSUserDefaults.standardUserDefaults().synchronize() secondsLabel.text = "\(Int(sender.value).description) seconds" } 

2 controller

 override func viewDidLoad() { super.viewDidLoad() if NSUserDefaults.standardUserDefaults().objectForKey("duretionExersise") != nil { duretionExersise = NSUserDefaults.standardUserDefaults().objectForKey("duretionExersise") as! Double } //Есть лэйбл в нем отображаем начальное значение таймера timerLabel.text = "\(Int(duretionExersise))" } var timer = NSTimer() var exersiseTime = Int(duretionExersise) var timerRunning = false var i = 0 func Counting(){ exersiseTime -= 1 timerLabelChange.text = "\(Int(exersiseTime))" if exersiseTime == -1 { i += 1 exersiseTime = Int(duretionExersise) timerLabelChange.text = "\(Int(exersiseTime))" } @IBAction func closeButton(sender: UIButton) { timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true) timerRunning = true } 
  • one
    After saving the value in NSUserDefaults you need to do the following: NSUserDefaults.standardUserDefaults().synchronize() - Vitali Eller
  • For some reason, the simulator works correctly only if before starting the timer just go to 1 controller without changing anything, and if you start the simulator immediately go to the controller with timer and start, the error remains - Artur Skachkov
  • your code pasted like this: @IBAction func secondsStepper (sender: UIStepper) {NSUserDefaults.standardUserDefaults (). setObject (Int (sender.value), forKey: "duretionExersise") NSUserDefaults.standardUserDefaults (.), to apply and use this). (Int (sender.value) .description) seconds "} - Artur Skachkov
  • There is such a problem with the simulator, when synchronize is not instantaneous, but with a delay of up to 10 seconds. Try to play on the device, or after saving, wait a few seconds before downloading. - Max Mikheyenko
  • Unfortunately, it didn’t help even to go and wait, but it didn’t help either, but when the app was started up and the timer went straight to the timer, the error remained - Artur Skachkov

1 answer 1

Missed the need to override the exersiseTime variable in the viewDidLoad () method. In 2 controller in viewDidLoad () you need to add exersiseTime = Int(duretionExersise)