我想知道如何为用户默认设置执行switch-case /任何好的声明而不是使用if-else,因为从我的代码中可以看出,我使用3 if语句来升级多余和丑陋。 这个视图控制器的目的是为了一个"选择一张卡"在游戏中,用户每次都可以选择3张牌,如果他/她选择了一张牌,他们将无法再次选择该牌。
import UIKit
class ViewController: UIViewController {
@IBOutlet var buttona: UIButton!
@IBOutlet var buttonb: UIButton!
@IBOutlet var buttonc: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if (UserDefaults.standard.integer(forKey: "dissapear") == 1)
{
buttona.setImage(UIImage(named: "wildcardbackingblack.png"), for: .normal)
buttona.isEnabled = false
}
if (UserDefaults.standard.integer(forKey: "dissapear2") == 1)
{
buttonb.setImage(UIImage(named: "wildcardbackingblack.png"), for: .normal)
buttonb.isEnabled = false
}
if (UserDefaults.standard.integer(forKey: "dissapear3") == 1)
{
buttonc.setImage(UIImage(named: "wildcardbackingblack.png"), for: .normal)
buttonc.isEnabled = false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Button1(_ sender: Any) {
UserDefaults.standard.set(1, forKey:"dissapear")
self.performSegue(withIdentifier: "show", sender: nil)
}
@IBAction func Button2(_ sender: Any) {
UserDefaults.standard.set(1, forKey:"dissapear2")
self.performSegue(withIdentifier: "show", sender: nil)
}
@IBAction func Button3(_ sender: Any) {
UserDefaults.standard.set(1, forKey:"dissapear3")
self.performSegue(withIdentifier: "show", sender: nil)
}
}
答案 0 :(得分:1)
func updateCard(button: UIButton, image: String, isEnabled: Bool) {
DispatchQueue.main.asyc {
button.setImage(UIImage(named:image), for: .normal)
button.isEnabled = isEnabled
}
}
.
.
.
let option : Int = UserDefaults.standard.integer(forKey: "dissapear")
switch (option) {
case 1:
self.updateCard(button: ..., image:"wildcardbackingblack.png", isEnabled: false)
case 2:
self.updateCard(button: ..., image:"wildcardbackingblack.png", isEnabled: false)
case 3:
self.updateCard(button: ..., image:"wildcardbackingblack.png", isEnabled: false)
default:
self.updateCard(button: ..., image:"default.png", isEnabled: false)
}
//Save the different integer value in same key user defaults.
@IBAction func Button1(_ sender: Any) {
UserDefaults.standard.set(1, forKey:"dissapear")
self.performSegue(withIdentifier: "show", sender: nil)
}
@IBAction func Button2(_ sender: Any) {
UserDefaults.standard.set(2, forKey:"dissapear")
self.performSegue(withIdentifier: "show", sender: nil)
}
@IBAction func Button3(_ sender: Any) {
UserDefaults.standard.set(3, forKey:"dissapear")
self.performSegue(withIdentifier: "show", sender: nil)
}
答案 1 :(得分:1)
如果我坚持使用相同的UserDefaults存储,我将如何编写此代码。注意:您需要将cardPressed(_:)
重新连接到每个按钮/卡
import UIKit
class ViewController: UIViewController {
@IBOutlet var buttonA: UIButton!
@IBOutlet var buttonB: UIButton!
@IBOutlet var buttonC: UIButton!
let buttonKeyPairs: [(button: UIButton, key: String)] = [
(buttonA, "dissapear"),
(buttonB, "dissapear2"),
(buttonC, "dissapear3"),
]
override func viewDidLoad() {
super.viewDidLoad()
for (button, key) in buttonKeyPairs
where UserDefaults.standard.integer(forKey: key) == 1 {
button.setImage(UIImage(named: "wildcardbackingblack.png"), for: .normal)
button.isEnabled = false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(_ sender: UIButton) {
let key: String
switch UIButton {
case buttonA: key = "dissapear"
case buttonB: key = "dissapear2"
case buttonC: key = "dissapear3"
}
UserDefaults.standard.set(1, forKey: key)
self.performSegue(withIdentifier: "show", sender: self)
}
}