简体   繁体   English

如何使用NSUserdefaults保存设置

[英]How do I save settings using NSUserdefaults

I am making a tip calculator and the requirement is to have a settings page to go along with the calculator. 我正在制作一个小费计算器,要求是要有一个设置页面才能与计算器一起使用。 In the calculator there are three options implemented by a segmented control when each is selected, the value of the tip and the value of the total change. 在计算器中,当选择每个选项时,由分段控件实现三个选项,即笔尖的值和总变化的值。 In my settings tab I would like to be able to have the user save their default tip percentage. 在我的设置标签中,我希望用户可以保存其默认小费百分比。 I know I need to use NSUserdefaults, however I do not know how to do this using two different pages (or one page for that matter). 我知道我需要使用NSUserdefaults,但是我不知道如何使用两个不同的页面(或一个页面)来执行此操作。 If what I want to achieve is unclear, please let me know I tried my best explaining it thoroughly. 如果我想实现的目标不清楚,请让我知道我已尽力彻底解释了它。

Here is the code for the view controller: 这是视图控制器的代码:

@IBAction func onEditingChanged(sender: AnyObject) {

    var tipPercentages = [0.18, 0.2, 0.22]

    let tipPercentage = tipPercentages[tipControl.selectedSegmentIndex]

    let billAmount = billField.text!._bridgeToObjectiveC().doubleValue

    let billAmt = billAmount

    let tip = billAmt * tipPercentage

    let total = billAmt + tip

    tipLabel.text = String(format:"$%.2f", tip)

    totalLabel.text = String(format:"$%.2f", total)

}

Here is what I have for the settingsViewController: 这是我的settingsViewController:

import UIKit

class SettingsViewController: UIViewController {

@IBOutlet weak var defaultTipControl: UISegmentedControl!



@IBAction func actDefaultTipCont(sender: AnyObject) {

    var tipPercentages = [0.18, 0.2, 0.22]

    var tipPercentage = [defaultTipControl.selectedSegmentIndex]

    let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setObject(tipPercentage, forKey: "tippingDefault")

    defaults.setInteger(123, forKey: "tippingInteger")

    defaults.synchronize()

}

I have just been messing around with the settingsViewController trying to get it to work, it will likely all have to be redone. 我只是在和settingsViewController搞混,试图使其正常工作,可能必须全部重做。 Here are pictures of the views of the two different pages: 这是两个不同页面的视图图片:

Entry View 条目视图

Settings Page 设定页面

Thank you ahead of time for the help! 提前谢谢您的帮助!

To save then retrieve the data, you want to do something like this: 要保存然后检索数据,您想要执行以下操作:

let valueToSave = tipAmount;
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(valueToSave, forKey: "tipAmount")

Then to retrieve it and use it on your main control (for example) do this: 然后检索它并在您的主控件上使用它(例如),执行以下操作:

let defaults = NSUserDefaults.standardUserDefaults()
tipAmount = defaults.objectForKey("tipAmount")

the valueForKey method returns whatever you saved in that key previously valueForKey方法返回您之前在该键中保存的任何内容

NSUserdefaults is basically just a dictionary that will persist over different launches, so just check if you have saved a value for a specific key to access it NSUserdefaults基本上只是一个字典,它将在不同的启动中持续存在,因此只需检查是否已保存了用于访问它的特定键的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM