There are 12 buttons, and each of them has 3 options for selecting pictures, depending on the data. It works but I think need a shorter and more intelligent decision.
import SpriteKit
class SelectEasyLevelViewController: UIViewController{
@IBOutlet weak var level_1: UIButton!
@IBOutlet weak var level_2: UIButton!
@IBOutlet weak var level_3: UIButton!
@IBOutlet weak var level_4: UIButton!
@IBOutlet weak var level_5: UIButton!
@IBOutlet weak var level_6: UIButton!
@IBOutlet weak var level_7: UIButton!
@IBOutlet weak var level_8: UIButton!
@IBOutlet weak var level_9: UIButton!
@IBOutlet weak var level_10: UIButton!
@IBOutlet weak var level_11: UIButton!
@IBOutlet weak var level_12: UIButton!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
//This thing help me to open data
if UserDefaults.standard.array(forKey: "levelPassed") != nil {
Model.sharedInstance.levelPassed = UserDefaults.standard.array(forKey: "levelPassed") as! [Int]
}
let image = UIImage(named: "levelbutton01")
let image2 = UIImage(named: "levelbutton02")
let image3 = UIImage(named: "levelbutton03")
switch Model.sharedInstance.levelPassed[0] {
case 1:
level1.setBackgroundImage(image, for: .normal)
case 2:
level1.setBackgroundImage(image2, for: .normal)
case 3:
level1.setBackgroundImage(image3, for: .normal)
default:
break
}
..........................................
Here codes for button 2, 4, 5, 6, 7, 8, 9, 10, 12
.........................................
switch Model.sharedInstance.levelPassed[11] {
case 1:
level_12.setBackgroundImage(image, for: .normal)
case 2:
level_12.setBackgroundImage(image2, for: .normal)
case 3:
level_12.setBackgroundImage(image3, for: .normal)
default:
break
}
}
Or maybe another way.
UIKit
,not sprite kit
\$\endgroup\$level1.setBackgroundImage(UIImage(named: "levelbutton\(String(format: "%02d", Model.sharedInstance.levelPassed[0]))"), for: .normal)
\$\endgroup\$