gpt4 book ai didi

swift - Xcode Swift 使用 Toggles 将 Label 增加 1 并保存 Toggles 的值

转载 作者:行者123 更新时间:2023-11-28 08:02:45 33 4
gpt4 key购买 nike

如果有任何反馈或见解可以帮助我解决我面临的最后一个问题,我将不胜感激。我的代码很庞大。

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

我知道代码的编写方式认为其他三个切换 = 0 并使标签显示为 1。

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

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
}
}

/image/co6zj.png /image/dJtcx.png

最佳答案

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

这是 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)
}

关于swift - Xcode Swift 使用 Toggles 将 Label 增加 1 并保存 Toggles 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46160169/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com