【发布时间】:2016-09-30 02:22:04
【问题描述】:
我正在开发一个基本应用程序的设置视图。基本,在用户的设置视图中只有一个开关。开关设置与 NSUserDefault 一起保存。我使用委托将开关信号从设置视图发送到主视图。委派工作正常。
用户界面是基本的。在主视图上,标签将显示为绿色的 On(如果开关打开)和 Off 为红色(如果开关关闭)。右上角有一个设置按钮,将 (settingsSegue) 设置为 UITableViewController , UISwitch 所在的位置。
问题是在应用加载后加载 NSUserDefault。在 viewDidLoad 中,我检查是否有为 switch 键保存的值。如果有,请加载它。如果不是,请将其设置为 false(在情节提要中,默认情况下开关设置为 false。)开关状态每次加载为关闭。即使默认值为 On。这不应该发生。
ViewController.swift:
import UIKit
var nsDefaults = NSUserDefaults.standardUserDefaults()
class ViewController: UIViewController, SettingsViewControllerDelegate {
var onFromMain = Bool()
@IBOutlet weak var switchStateLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if let mySavedKey = nsDefaults.objectForKey("savedSwitchSettingDefault") {
// A value exists. Load it up.
nsDefaults.objectForKey("savedSwitchSettingDefault")
print("The switch is set! \(mySavedKey)")
checkSwitchState()
}
else {
// Nothing stored in NSUserDefaults yet. Set switch to False.
nsDefaults.setBool(false, forKey: "savedSwitchSettingDefault")
checkSwitchState()
}
}
func myVCDidFinish(controller: SettingsViewController, switchState: Bool) {
onFromMain = switchState.boolValue
checkSwitchState()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "settingsSegue" {
let nav = segue.destinationViewController as! UINavigationController
let secondVC = nav.topViewController as! SettingsViewController
secondVC.delegate = self
}
}
func checkSwitchState() {
if onFromMain {
switchStateLabel.text = "On"
switchStateLabel.textColor = UIColor.greenColor()
}
else {
switchStateLabel.text = "Off"
switchStateLabel.textColor = UIColor.redColor()
}
}
}
SettingsViewController.swift:
import UIKit
protocol SettingsViewControllerDelegate {
func myVCDidFinish(controller: SettingsViewController, switchState: Bool)
}
class SettingsViewController: UITableViewController {
var delegate: SettingsViewControllerDelegate? = nil
@IBOutlet weak var switchOutlet: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
switchOutlet.on = nsDefaults.boolForKey("savedSwitchSettingDefault")
}
@IBAction func closeSettingsPageBarButtonItemPressed(sender: UIBarButtonItem) {
if (delegate != nil) {
delegate!.myVCDidFinish(self, switchState: switchOutlet.on)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
@IBAction func switchPressed(sender: UISwitch) {
// Tap the switch to change the setting.
nsDefaults.setBool(switchOutlet.on, forKey: "savedSwitchSettingDefault")
}
}
我相信我的问题在于加载“savedSwitchSettingDefault”的默认键。它是否正确?还是问题出在代码的其他地方?
【问题讨论】:
-
您将保存的值提取到
mySavedKey,但随后不对该变量执行任何操作。然后你无缘无故地再次拨打objectForkey,无论如何你都应该使用boolForKey -
@Paulw11 - 我更改了 if 语句以删除可选绑定。没有更多无用的财产。还在语句中将 objectForKey 更改为 boolForKey。我的评论中说明了“objectForKey”的原因。我正在检查它的值是否已经存在。该应用程序应该可以正常运行,而用户无需在设置中更改 UISwitch。现在,它没有。这一切有意义吗?谢谢!
-
如果你使用
boolForKey,如果密钥不存在,你只会得到false,无论如何这是你的默认值。第一次设置为true时会保存一个值,所以不需要整个if nil的东西。
标签: ios swift settings nsuserdefaults