Xcode Swift通过Toggles和Toggles的保存值将Label增加1

时间:2017-09-11 16:19:47

标签: swift switch-statement toggle increment

我非常感谢任何反馈或见解,以帮助解决我面临的最后一个问题。我的代码很笨重。

我有4个切换。打开时每个切换都会使标签增加1.我还设法保存切换位置并在关闭和重新打开应用程序时标记#。但是,当我关闭并重新打开应用程序时,然后切换其中一个切换,标签将返回1.

我理解编写代码的方式认为其他三个切换= 0并使标签说明1.

有没有一种方法可以让每个帧都运行一个函数,不断检查哪些切换是ON / OFF然后在标签中返回该值,还可以在我打开和关闭App时保存所有信息。这是下面的图片和我的代码。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var switch1: UISwitch!
    @IBOutlet weak var switch2: UISwitch!
    @IBOutlet weak var switch3: UISwitch!
    @IBOutlet weak var switch4: UISwitch!

    var v1 = 0
    var v2 = 0
    var v3 = 0
    var v4 = 0

    var s1 = "s1"
    var s2 = "s2"
    var s3 = "s3"
    var s4 = "s4"

    var l1 = "l1"

    var defaults = UserDefaults.standard

    @IBAction func switch1On(_ sender: UISwitch) {
        if switch1.isOn {
            v1 = 1
            label.text = String(v1 + v2 + v3 + v4)
        }
        else {
            v1 = 0
            label.text = String(v1 + v2 + v3 + v4)
        }
    }

    @IBAction func switch2On(_ sender: UISwitch) {
        if switch2.isOn {
            v2 = 1
            label.text = String(v1 + v2 + v3 + v4)
        }
        else {
            v2 = 0
            label.text = String(v1 + v2 + v3 + v4)
        }
    }

    @IBAction func switch3On(_ sender: UISwitch) {
        if switch3.isOn {
            v3 = 1
            label.text = String(v1 + v2 + v3 + v4)
        }
        else {
            v3 = 0
            label.text = String(v1 + v2 + v3 + v4)
        }
    }

    @IBAction func switch4On(_ sender: UISwitch) {
        if switch4.isOn {
            v4 = 1
            label.text = String(v1 + v2 + v3 + v4)
        }
        else {
            v4 = 0
            label.text = String(v1 + v2 + v3 + v4)
        }
    }

    @IBAction func switch1Changed(_ sender: UISwitch) {
        defaults.set(sender.isOn, forKey: s1)
        defaults.set(label.text, forKey: l1)
    }

    @IBAction func switch2Changed(_ sender: UISwitch) {
        defaults.set(sender.isOn, forKey: s2)
        defaults.set(label.text, forKey: l1)
    }

    @IBAction func switch3Changed(_ sender: UISwitch) {
        defaults.set(sender.isOn, forKey: s3)
        defaults.set(label.text, forKey: l1)
    }

    @IBAction func switch4Changed(_ sender: UISwitch) {
        defaults.set(sender.isOn, forKey: s4)
        defaults.set(label.text, forKey: l1)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if switch1.isOn {
             v1 = 1
        }
        if switch2.isOn {
             v2 = 1
        }
        if switch3.isOn {
             v3 = 1
        }
        if switch4.isOn {
             v4 = 1
        }
        label.text = String(v1 + v2 + v3 + v4)

        if let s1change = defaults.value(forKey: s1) {
            switch1.isOn = s1change as! Bool
        }
        if let s2change = defaults.value(forKey: s2) {
            switch2.isOn = s2change as! Bool
        }
        if let s3change = defaults.value(forKey: s3) {
            switch3.isOn = s3change as! Bool
        }
        if let s4change = defaults.value(forKey: s4) {
            switch4.isOn = s4change as! Bool
        }
        if let labelChange = defaults.value(forKey: l1) {
            label.text = labelChange as? String
        }
    }

https://i.stack.imgur.com/co6zj.png https://i.stack.imgur.com/dJtcx.png

1 个答案:

答案 0 :(得分:0)

绝对认为这可以更有说服力,但我已经解决了这个问题。

这是10个开关的代码,用于逐步增加标签1并在应用程序打开和关闭时保存开关的值。

import UIKit

class ViewController: UIViewController {


var timer1 = Timer()
var timer4 = Timer()



var v1 = 0
var v2 = 0
var v3 = 0
var v4 = 0
var v5 = 0
var v6 = 0
var v7 = 0
var v8 = 0
var v9 = 0
var v10 = 0


var s1 = "s1"
var s2 = "s2"
var s3 = "s3"
var s4 = "s4"
var s5 = "s5"
var s6 = "s6"
var s7 = "s7"
var s8 = "s8"
var s9 = "s9"
var s10 = "s10"


var l1 = "l1"



var defaults = UserDefaults.standard


@IBOutlet weak var label1: UILabel!


@IBOutlet weak var switch1: UISwitch!
@IBOutlet weak var switch2: UISwitch!
@IBOutlet weak var switch3: UISwitch!
@IBOutlet weak var switch4: UISwitch!
@IBOutlet weak var switch5: UISwitch!
@IBOutlet weak var switch6: UISwitch!
@IBOutlet weak var switch7: UISwitch!
@IBOutlet weak var switch8: UISwitch!
@IBOutlet weak var switch9: UISwitch!
@IBOutlet weak var switch10: UISwitch!




@IBAction func switch1Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s1)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch2Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s2)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch3Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s3)
    defaults.set(label1.text, forKey: l1)

}
@IBAction func switch4Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s4)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch5Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s5)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch6Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s6)
    defaults.set(label1.text, forKey: l1)

}
@IBAction func switch7Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s7)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch8Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s8)
    defaults.set(label1.text, forKey: l1)

}

@IBAction func switch9Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s9)
    defaults.set(label1.text, forKey: l1)

}
@IBAction func switch10Changed(_ sender: UISwitch) {
    defaults.set(sender.isOn, forKey: s10)
    defaults.set(label1.text, forKey: l1)

}




override func viewDidLoad() {
    timer1 = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(switchCheck1), userInfo: nil, repeats: true)

    timer4 = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(sumTotal), userInfo: nil, repeats: true)

    if let s1change = defaults.value(forKey: s1) {
        switch1.isOn = s1change as! Bool
    }
    if let s2change = defaults.value(forKey: s2) {
        switch2.isOn = s2change as! Bool
    }
    if let s3change = defaults.value(forKey: s3) {
        switch3.isOn = s3change as! Bool
    }
    if let s4change = defaults.value(forKey: s4) {
        switch4.isOn = s4change as! Bool
    }
    if let s5change = defaults.value(forKey: s5) {
        switch5.isOn = s5change as! Bool
    }
    if let s6change = defaults.value(forKey: s6) {
        switch6.isOn = s6change as! Bool
    }
    if let s7change = defaults.value(forKey: s7) {
        switch7.isOn = s7change as! Bool
    }
    if let s8change = defaults.value(forKey: s8) {
        switch8.isOn = s8change as! Bool
    }
    if let s9change = defaults.value(forKey: s9) {
        switch9.isOn = s9change as! Bool
    }
    if let s10change = defaults.value(forKey: s10) {
        switch10.isOn = s10change as! Bool
    }
    if let labelChange = defaults.value(forKey: l1) {
        label1.text = labelChange as? String
    }


}

func switchCheck1() {
    if switch1.isOn {
        v1 = 1
    }
    else {
        v1 = 0}

    if switch2.isOn {
        v2 = 1
    }
    else {
        v2 = 0}

    if switch3.isOn {
        v3 = 1
    }
    else {
        v3 = 0}
    if switch4.isOn {
        v4 = 1
    }
    else {
        v4 = 0}
    if switch5.isOn {
        v5 = 1
    }
    else {
        v5 = 0}
    if switch6.isOn {
        v6 = 1
    }
    else {
        v6 = 0}
    if switch7.isOn {
        v7 = 1
    }
    else {
        v7 = 0}
    if switch8.isOn {
        v8 = 1
    }
    else {
        v8 = 0}
    if switch9.isOn {
        v9 = 1
    }
    else {
        v9 = 0}
    if switch10.isOn {
        v10 = 1
    }
    else {
        v10 = 0}
}




func sumTotal() {
    label1.text = String(v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10)
}

}