如何使用NSUserdefaults保存设置

时间:2020-11-25 16:51:58

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). If what I want to achieve is unclear, please let me know I tried my best explaining it thoroughly.

我正在制作小费计算器,并且要求设置页面与计算器一起使用。在计算器中,当选择每个选项时,分段控件实现三个选项,尖端的值和总变化的值。在我的设置标签中,我希望能够让用户保存默认的提示百分比。我知道我需要使用NSUserdefaults,但是我不知道如何使用两个不同的页面(或者就此而言一页)。如果我想要达到的目标不清楚,请告诉我,我已尽力解释。

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. Here are pictures of the views of the two different pages:

我刚刚使用settingsViewController试图让它工作,它可能都必须重做。以下是两个不同页面的视图图片:

Entry View

Settings Page

Thank you ahead of time for the help!

提前谢谢你的帮助!

1 个解决方案

#1


0  

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基本上只是一个字典,它将持续不同的启动,所以只需检查是否已保存特定键的值以访问它

#1


0  

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基本上只是一个字典,它将持续不同的启动,所以只需检查是否已保存特定键的值以访问它