如何根据swift中的随机数生成显示某个视图?

时间:2017-06-30 13:03:56

标签: swift match views

我正在关注教程,这是git

上的回购

我有7个视图,代表模具。还有6个标签,每次按下dieButtonTapped()按钮时都显示随机数。当我按下按钮时,第一个标签出现时会生成随机数,这很好,但我无法获得适当的视图显示。因此,如果随机数为5,则标签显示其中的数字5,并且7中的5个视图应显示,表示模具。 它现在正在做的是它显示的随机视图与标签上显示的随机数不匹配。

请帮助我更正func rearrangeDie()功能以匹配func updateScore(roll: Int)返回的内容。

谢谢! 这是我的代码:

class ViewController: UIViewController {


    @IBOutlet weak var view1: UIView!
    @IBOutlet weak var view2: UIView!
    @IBOutlet weak var view3: UIView!
    @IBOutlet weak var view4: UIView!
    @IBOutlet weak var view5: UIView!
    @IBOutlet weak var view6: UIView!
    @IBOutlet weak var view7: UIView!

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!



    override func viewDidLoad() {
        super.viewDidLoad()

        view1.isHidden = true
        view2.isHidden = true
        view3.isHidden = true
        view4.isHidden = true
        view5.isHidden = true
        view6.isHidden = true
        view7.isHidden = true

        label1.isHidden = true
        label2.isHidden = true
        label3.isHidden = true
        label4.isHidden = true
        label5.isHidden = true
        label6.isHidden = true

    }

    @IBAction func dieButtonTapped(_ sender: AnyObject) {

        rearrangeDie()
        updateScore(roll: randomDiceRoll())
    }

    // Returns back a random Int (1, 2, 3, 4, 5, or 6)
    func randomDiceRoll() -> Int {
        return Int(arc4random_uniform(6) + 1)

    }

    func rearrangeDie() {

        if randomDiceRoll() == 1 {
            view7.isHidden = false
        }
        else if randomDiceRoll() == 2 {
            view2.isHidden = false
            view5.isHidden = false
        }
        else if randomDiceRoll() == 3 {
            view2.isHidden = false
            view7.isHidden = false
            view5.isHidden = false
        }
        else if randomDiceRoll() == 4 {
            view1.isHidden = false
            view2.isHidden = false
            view5.isHidden = false
            view6.isHidden = false
        }
        else if randomDiceRoll() == 5 {
            view1.isHidden = false
            view2.isHidden = false
            view5.isHidden = false
            view6.isHidden = false
            view7.isHidden = false
        }
        else if randomDiceRoll() == 6 {
            view1.isHidden = false
            view2.isHidden = false
            view3.isHidden = false
            view4.isHidden = false
            view5.isHidden = false
            view6.isHidden = false
        }


    }


    func updateScore(roll: Int) {

        let diceScore = String(roll)

        if label1.isHidden {
            label1.text = diceScore
            label1.isHidden = false

        }
        else if label2.isHidden {
            label2.text = diceScore
            label2.isHidden = false

        }
        else if label3.isHidden {
            label3.text = diceScore
            label3.isHidden = false

        }
        else if label4.isHidden {
            label4.text = diceScore
            label4.isHidden = false

        }
        else if label5.isHidden {
            label5.text = diceScore
            label5.isHidden = false

        }
        else if label6.isHidden {
            label6.text = diceScore
            label6.isHidden = false

        }
        else {
            viewDidLoad()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

每次拨打randomDiceRoll()时,您基本上都会生成一个新的随机数。将其存储在变量中,并在if语句中访问它。

我还强烈建议以编程方式创建布局并进行严格的重构。

func rearrangeDie() {
    let randomDiceRoll = randomDiceRoll()

    if randomDiceRoll == 1 {
        view7.isHidden = false
    }
    else if randomDiceRoll == 2 {
        view2.isHidden = false
        view5.isHidden = false
    }
    else if randomDiceRoll == 3 {
        view2.isHidden = false
        view7.isHidden = false
        view5.isHidden = false
    }
    else if randomDiceRoll == 4 {
        view1.isHidden = false
        view2.isHidden = false
        view5.isHidden = false
        view6.isHidden = false
    }
    else if randomDiceRoll == 5 {
        view1.isHidden = false
        view2.isHidden = false
        view5.isHidden = false
        view6.isHidden = false
        view7.isHidden = false
    }
    else if randomDiceRoll == 6 {
        view1.isHidden = false
        view2.isHidden = false
        view3.isHidden = false
        view4.isHidden = false
        view5.isHidden = false
        view6.isHidden = false
    }
}

答案 1 :(得分:0)

@ the4Kman说明调用randomDiceRoll()每次都会生成一个新的随机数,这会导致你遇到的问题。您需要的是将rearrangeDie()updateScore()方法合并为一个。另外,正如@ n​​anothread59建议的那样,您应该使用IBOutletCollections

您应该声明collections这样的

//Notice that you cannot make them "weak" properties
@IBOutlet var views: Array<UIView>!
@IBOutlet var labels: Array<UILabel>!

然后在故事板中为每个viewlabel提供1-7的标记,并将它们连接到各自的outletCollections。设置完成后,您可以将方法合并为一个类似

的方法
func update() {
    //Hide all Views and Labels
    for item in views {
        item.isHidden = true
    }
    for item in labels {
        item.isHidden = true
    }

    //Get Random Value
    let random = randomDiceRoll()

    //UPDATE YOUR VIEWS' AND LABELS' HIDDEN PROPERTY AND VALUES HERE BASED ON THE RANDOM VALUE

    //To access a particular view or label, you'll do something like this
    for item in views {
        if item.tag == {whatever} {
            //DO SOMETHING
        }
    }
    //Repeat the above loop for labels as well
}

P.S。使用IBOutletCollections,您的viewDidLoad也会像这样更改

override func viewDidLoad() {
    super.viewDidLoad()
    //Hide all Views and Labels
    for item in views {
        item.isHidden = true
    }
    for item in labels {
        item.isHidden = true
    }
}