如何使用标签中的用户输入打印随机结果?

问题描述 投票:1回答:1

单击按钮尝试随机显示已使用数据输入的两个标签中的一个。我现在能够获得第一组代码,随机选择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
    }
arrays button label arc4random
1个回答
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()


    }
© www.soinside.com 2019 - 2024. All rights reserved.