x如何在每次退出应用程序时保存UISwitch状态(Swift) [英] xHow to save UISwitch state whenever the application is quit (Swift)

查看:198
本文介绍了x如何在每次退出应用程序时保存UISwitch状态(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存多个开关的状态,因此当退出并重新打开应用程序时,所有开关的状态都不相同,即它们退出前的位置.这是我为快速家庭作业经理准备的非常简单的代码.

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


}

推荐答案

AppDelegate类的applicationDidEnterBackground中发布通知,以便当应用程序进入后台时可以通知您的视图控制器:

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);   
}

在您的viewcontroller类中添加以下代码:

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");
}

首先,将viewcontroller添加为应用程序在后台运行时发布的通知的观察者.触发此通知后,它将调用saveSwitchesStates()方法,该方法会将开关状态保存在NSUserDefaults中.同样在viewDidLoad()调用restoreSwitchesStates(),它将从NSUserDefaults

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

这篇关于x如何在每次退出应用程序时保存UISwitch状态(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆