问题
How can I save the state of multiple switches so when the application is quit and reopened, all of the switches are not at the same state they where before I quit it. Here is my very simple code for a quick homework manager I did.
//
// ViewController.swift
// HomeworkManager
//
// Created by Nate Parker on 9/2/14.
// Copyright (c) 2014 Nathan Parker. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func resetClicked(sender: AnyObject) {
spanish.setOn(false, animated: true);
algebra.setOn(false, animated: true);
amerCult.setOn(false, animated: true);
bio.setOn(false, animated: true);
col.setOn(false, animated: true);
}
@IBOutlet var spanish: UISwitch
@IBOutlet var algebra: UISwitch
@IBOutlet var amerCult: UISwitch
@IBOutlet var bio: UISwitch
@IBOutlet var col: UISwitch
}
回答1:
In AppDelegate
class, in applicationDidEnterBackground
post a notification, so your view controller will be able to be notified when app goes in background:
func applicationDidEnterBackground(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("kSaveSwitchesStatesNotification", object: nil);
}
In your viewcontroller class add this code:
override func viewDidLoad() {
super.viewDidLoad()
self.restoreSwitchesStates();
NSNotificationCenter.defaultCenter().addObserver(self, selector: "saveSwitchesStates", name: "kSaveSwitchesStatesNotification", object: nil);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func saveSwitchesStates() {
NSUserDefaults.standardUserDefaults().setBool(spanish!.on, forKey: "spanish");
NSUserDefaults.standardUserDefaults().setBool(algebra!.on, forKey: "algebra");
NSUserDefaults.standardUserDefaults().setBool(amerCult!.on, forKey: "amerCult");
NSUserDefaults.standardUserDefaults().setBool(bio!.on, forKey: "bio");
NSUserDefaults.standardUserDefaults().setBool(col!.on, forKey: "col");
NSUserDefaults.standardUserDefaults().synchronize();
}
func restoreSwitchesStates() {
spanish!.on = NSUserDefaults.standardUserDefaults().boolForKey("spanish");
algebra!.on = NSUserDefaults.standardUserDefaults().boolForKey("algebra");
amerCult!.on = NSUserDefaults.standardUserDefaults().boolForKey("amerCult");
bio!.on = NSUserDefaults.standardUserDefaults().boolForKey("bio");
col!.on = NSUserDefaults.standardUserDefaults().boolForKey("col");
}
First thing add viewcontroller as an observer for the notification posted when app goes in background. When this notification will be triggered it will call saveSwitchesStates()
method which will save switches states in NSUserDefaults
. Also in viewDidLoad()
call restoreSwitchesStates()
which will read stored bools for switches states from NSUserDefaults
回答2:
Use NSUserDefaults
to save there state when application is going to close and load them back when the app launches.
Tutorial explaining how to save data using NSUserDefaults
http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
来源:https://stackoverflow.com/questions/25652095/xhow-to-save-uiswitch-state-whenever-the-application-is-quit-swift