应用程序的要点很简单,你按下小骰子图像,它运行一个从1-6返回一个随机数的函数,屏幕上的方框反映了它在骰子上的样子,以及彩色掷骰子时,框号标签会随着数字更新。当我运行应用程序时,一切都看起来很好。一旦我按下骰子按钮,randomDiceRoll()函数就会运行,但彩色框标签和UIView框(反映它在骰子上的样子)不匹配。再一次,我只是学习Swift,所以请给我一些懈怠。感谢所有贡献者!这是代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var upperLeft: UIView!
@IBOutlet var midLeft: UIView!
@IBOutlet var lowerLeft: UIView!
@IBOutlet var middle: UIView!
@IBOutlet var upperRight: UIView!
@IBOutlet var midRight: UIView!
@IBOutlet var lowerRight: UIView!
@IBOutlet var redBox: UILabel!
@IBOutlet var orangeBox: UILabel!
@IBOutlet var yellowBox: UILabel!
@IBOutlet var greenBox: UILabel!
@IBOutlet var blueBox: UILabel!
@IBOutlet var purpleBox: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
redBox.isHidden = true
orangeBox.isHidden = true
yellowBox.isHidden = true
greenBox.isHidden = true
blueBox.isHidden = true
purpleBox.isHidden = true
}
func randomDiceRoll() -> Int {
return Int(arc4random_uniform(6) + 1)
}
func updateLabelWithRoll(roll: Int) {
let diceScore = String(roll)
if redBox.isHidden {
redBox.text = diceScore
redBox.isHidden = false
} else if orangeBox.isHidden {
orangeBox.text = diceScore
orangeBox.isHidden = false
} else if yellowBox.isHidden {
yellowBox.text = diceScore
yellowBox.isHidden = false
} else if greenBox.isHidden {
greenBox.text = diceScore
greenBox.isHidden = false
} else if blueBox.isHidden {
blueBox.text = diceScore
blueBox.isHidden = false
} else if purpleBox.isHidden {
purpleBox.text = diceScore
purpleBox.isHidden = false
} else {
redBox.isHidden = true
orangeBox.isHidden = true
yellowBox.isHidden = true
greenBox.isHidden = true
blueBox.isHidden = true
purpleBox.isHidden = true
}
}
@IBAction func buttonPress(_ sender: Any) {
randomDiceRoll()
updateLabelWithRoll(roll: randomDiceRoll())
if randomDiceRoll() == 1 {
upperLeft.isHidden = true
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = true
middle.isHidden = false
}
if randomDiceRoll() == 2 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
}
if randomDiceRoll() == 3 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
}
if randomDiceRoll() == 4 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
}
if randomDiceRoll() == 5 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
}
if randomDiceRoll() == 6 {
upperLeft.isHidden = false
midLeft.isHidden = false
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = false
lowerRight.isHidden = false
middle.isHidden = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
当应用加载时:
按一下按钮后:
按下两个按钮后:
依此类推......(app重复自己) 总共会有6个彩色标签盒。
答案 0 :(得分:5)
我认为您的问题是您多次调用randomDiceRoll()
函数而不是一次并使用结果。每次拨打randomDiceRoll()
时,它都会返回一个新的随机数,您应该调用一次,并将结果保存在内存中进行比较
重构代码,以便将函数的结果赋给变量并使用变量进行比较,如此
@IBAction func buttonPress(_ sender: Any) {
let score = randomDiceRoll()
updateLabelWithRoll(roll: score)
if score == 1 {
upperLeft.isHidden = true
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = true
middle.isHidden = false
}
if score == 2 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
}
if score == 3 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
}
if score == 4 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
}
if score == 5 {
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
}
if score == 6 {
upperLeft.isHidden = false
midLeft.isHidden = false
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = false
lowerRight.isHidden = false
middle.isHidden = true
}
}
我还建议使用switch语句而不是多个if语句..
@IBAction func buttonPress(_ sender: Any) {
let score = randomDiceRoll()
updateLabelWithRoll(roll: score)
switch score {
case 1:
upperLeft.isHidden = true
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = true
middle.isHidden = false
case 2:
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
case 3:
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = true
upperRight.isHidden = true
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
case 4:
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = true
case 5:
upperLeft.isHidden = false
midLeft.isHidden = true
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = true
lowerRight.isHidden = false
middle.isHidden = false
case 6:
upperLeft.isHidden = false
midLeft.isHidden = false
lowerLeft.isHidden = false
upperRight.isHidden = false
midRight.isHidden = false
lowerRight.isHidden = false
middle.isHidden = true
default:
break
}
}