在Swift 2中使用UISwitch保存NSUserDefaults

时间:2015-10-07 17:30:03

标签: nsuserdefaults swift2 uiswitch

我已经发布了下面的所有代码,以充分展示我想要实现的目标。我有一个UI Switch,我想在他们想要的游戏模式下保存NSUserDefault。我不确定为什么这不能正常工作,因为它目前无法保存 - 因为开关无法正常工作。

import SpriteKit

class ModeScene: SKScene {

    var modeSwitch = UISwitch()
    var motionLabel = UILabel()
    var touchLabel = UILabel()

    override func didMoveToView(view: SKView) {
        let MotionMode = NSUserDefaults.standardUserDefaults()
        let TouchMode = NSUserDefaults.standardUserDefaults()

        modeSwitch.center = CGPoint(x: view.center.x, y: view.frame.size.height / 1.3)
        modeSwitch.addTarget(self, action: Selector("updateMode"), forControlEvents: UIControlEvents.ValueChanged)
        if TouchMode {
            modeSwitch.on = false
        }
        else{
            modeSwitch.on = true
        }
        self.view?.addSubview(modeSwitch)

        motionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
        motionLabel.center = CGPoint(x: view.center.x * 3.4, y: view.frame.size.height / 1.3)
        motionLabel.text = "Accelerometer"
        motionLabel.textColor = UIColor.whiteColor()
        motionLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
        self.view?.addSubview(motionLabel)

        touchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
        touchLabel.center = CGPoint(x: view.center.x * 2.25, y: view.frame.size.height / 1.3)
        touchLabel.text = "Touch To Move"
        touchLabel.textColor = UIColor.whiteColor()
        touchLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
        self.view?.addSubview(touchLabel)

        DoneBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
        DoneBtn.center = CGPoint(x: view.center.x * 1.7, y: view.frame.size.height / 14)
        DoneBtn.setTitle("Done", forState: UIControlState.Normal)
        DoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        DoneBtn.addTarget(self, action: Selector("done"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view?.addSubview(DoneBtn)

        updateMode()

    }

    func updateMode(){
        if modeSwitch.on {
            // mode is accel
            let MotionMode = NSUserDefaults.standardUserDefaults()
            MotionMode.setValue(true, forKey: "MotionMode")
            NSLog("Mode is accel")

        }
        else{
            // mode is touch
            let TouchMode = NSUserDefaults.standardUserDefaults()
            TouchMode.setObject(true, forKey: "TouchMode")
            NSLog("Mode is touch")
        }
    }

    func done(){
        self.scene?.view?.presentScene(StartScene(size: self.scene!.size), transition: SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1))
        headerLabel.removeFromSuperview()
        DoneBtn.removeFromSuperview()
        modeSwitch.removeFromSuperview()
        touchLabel.removeFromSuperview()
        motionLabel.removeFromSuperview()
    }
}

代码更新:

import SpriteKit

class ModeScene: SKScene {

    var modeSwitch = UISwitch()
    var motionLabel = UILabel()
    var touchLabel = UILabel()
    var DoneBtn : UIButton!

    override func didMoveToView(view: SKView) {

        backgroundColor = SKColor.blackColor()

        modeSwitch.center = CGPoint(x: view.center.x, y: view.frame.size.height / 1.3)
        modeSwitch.addTarget(self, action: Selector("updateMode"), forControlEvents: UIControlEvents.ValueChanged)
        self.view?.addSubview(modeSwitch)

        motionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
        motionLabel.center = CGPoint(x: view.center.x * 3.4, y: view.frame.size.height / 1.3)
        motionLabel.text = "Accelerometer"
        motionLabel.textColor = UIColor.whiteColor()
        motionLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
        self.view?.addSubview(motionLabel)

        touchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
        touchLabel.center = CGPoint(x: view.center.x * 2.25, y: view.frame.size.height / 1.3)
        touchLabel.text = "Touch To Move"
        touchLabel.textColor = UIColor.whiteColor()
        touchLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
        self.view?.addSubview(touchLabel)

        DoneBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
        DoneBtn.center = CGPoint(x: view.center.x * 1.7, y: view.frame.size.height / 14)
        DoneBtn.setTitle("Done", forState: UIControlState.Normal)
        DoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        DoneBtn.addTarget(self, action: Selector("done"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view?.addSubview(DoneBtn)


        let defaults = NSUserDefaults.standardUserDefaults()
        if let motionMode = defaults.objectForKey("motionMode") as? Bool {
            if motionMode {
                // true
                modeSwitch.on = true
            } else {
                // false
                modeSwitch.on = false
            }
        }

        updateMode()

    }


    func updateMode(){
        let defaults = NSUserDefaults.standardUserDefaults()
        if modeSwitch.on {
            // mode is accel
            defaults.setBool(true, forKey: "motionMode")
            NSLog("Mode is accel")

        }
        else{
            // mode is touch
            defaults.setBool(true, forKey: "touchMode")
            NSLog("Mode is touch")
        }
    }


    func done(){
        self.scene?.view?.presentScene(StartScene(size: self.scene!.size), transition: SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1))
        headerLabel.removeFromSuperview()
        DoneBtn.removeFromSuperview()
        modeSwitch.removeFromSuperview()
        touchLabel.removeFromSuperview()
        motionLabel.removeFromSuperview()
    }
}

1 个答案:

答案 0 :(得分:1)

您忘记实际加载NSUserDefaults的值。

您可以使用setValuesetObject进行设置,但不要将其加载回来。

使用valueForKeyobjectForKey方法检索您的值。

let defaults = NSUserDefaults.standardUserDefaults()
let MotionMode = defaults.objectForKey("MotionMode") as! Bool
if MotionMode {
    // true
} else {
    // false
}

安全展开会更好:

let defaults = NSUserDefaults.standardUserDefaults()
if let MotionMode = defaults.objectForKey("MotionMode") as? Bool {
    if MotionMode {
        // true
    } else {
        // false
    }
}

一些注意事项:变量应以小写字母开头,因此" MotionMode"应该是" motionMode"等。您可以使用像boolForKey这样的专用对象getter而不是通用objectForKey来强制您对Bool进行类型转换。当然,另一方面它与setBool一致。最后:你调用的变量" MotionMode"实际上持有NSUserDefaults.standardUserDefaults(),所以我称之为"默认"而是在我的例子中。

您的问题有几个部分,但为单个问题制作一篇文章会更好。所以我专注于失败的NSUserDefaults部分,因为它是最重要的。使用我的答案,如果您的代码由于UI部分仍然无法正常工作,那么您应该发布一个新的不同问题来保持清晰。