如何在用户输入标签的情况下打印随机结果?

时间:2018-09-04 16:28:38

标签: arrays button label arc4random

通过单击按钮尝试随机显示已输入数据的两个标签中的一个。现在,我可以获得第一套工作于随机选择的2个标签中的1个的代码。但是,在代码的后半部分,当3个标签保持值时,它现在将打印3个标签中的2个。对于这两种方法,我只希望有一个结果。请帮忙!到目前为止,这是我的代码-

              //**UPDATE**                                                                         
@IBAction func decideBttn(_ sender: Any) {

        // if there is data in more than one Label randomly pick 1 out of 2
        if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == true
        {



            var topics = [valueLbl1.text!, valueLbl2.text!]


             pickTopic = Int(arc4random_uniform(UInt32(topics.count-0)))
            topics.remove(at: pickTopic)

            resultLbl.text = "\(topics)"

            valueLbl1.text = ""
            valueLbl2.text = ""
            valueLbl3.text = ""
            return
        }

           // if all 3 Labels are used button will randomly pick 1 out of 3
      else if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == false

      {
        var topics = [valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]



     pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
      topics.remove(at:pickTopic)
      resultLbl.text = "\(topics)"
       // resetting variable value
     valueLbl1.text = ""
    valueLbl2.text = ""
     valueLbl3.text = ""

     return
    }

1 个答案:

答案 0 :(得分:0)

想为那些想知道的人弄清楚。

 @IBAction func decideBttn(_ sender: Any) {


     // if there is data in more than one Label randomly pick 1 out of 2

    if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false
        {
            var topics = [valueLbl1.text!, valueLbl2.text!]

            pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
            topics.remove(at: pickTopic)
            resultLbl.text = "\(topics[pickTopic])"
            valueLbl1.text = ""
            valueLbl2.text = ""

            return ()

        }


           // if all 3 Labels are used button will randomly pick 1 out of 3


       else if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == false

      {
        var topics = [ valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]


     pickTopic = Int(arc4random_uniform(UInt32(topics.count)))

     topics.remove(at: pickTopic)
    resultLbl.text = "\(topics[pickTopic])"
    // resetting variable value
    valueLbl1.text = ""
    valueLbl2.text = ""
    valueLbl3.text = ""

   return()


    }