How do i keep UISwitch state when changing ViewControllers?

前端 未结 3 1201
予麋鹿
予麋鹿 2020-12-01 20:22

When I move from one view controller to another, the switch on the first controller resets itself and does not retain its state. How can I make it save its state when come

3条回答
  •  再見小時候
    2020-12-01 21:03

    Since you're syncing the on/off state of the switch you could on viewWillAppear: or viewDidAppear: set the state of the switch to the value stored in NSUserDefaults

    Something along the lines of

    override func viewWillAppear(animated: Bool) {
      self.switchButton.on = NSUserDefaults.standardUserDefaults().boolForKey(switchKey)
    }
    

提交回复
热议问题