简体   繁体   中英

How to register more than one button tap using UIKit in Swift?

I am experimenting with buttons, and I have run into what is probably a simple problem. I have two buttons and two labels.

The labels generate random string values of either "A" or "B". I want the correct label to disappear if the appropriate button is selected.

I have come up with the following code, but I have run into a problem. If the letters are the same, both labels will be hidden when the corresponding button is tapped.

I understand why this is happening, I think. It's because my code is executed when buttonA is tapped once(I haven't started on button B yet, so it doesn't do anything.)

So my question is how do I require 2 taps? In other words, if label_1 and label_2 are both displayed as String "A", how would i require the user to tap buttonA twice? If more code is needed, let me know in the comments.

    @IBOutlet weak var label_1: UILabel!
    @IBOutlet weak var label_2: UILabel!
    @IBOutlet weak var label_3: UILabel!
    @IBOutlet weak var label_4: UILabel!
    @IBOutlet weak var label_5: UILabel!


    var visibleLetters = ["A", "B", "Z", "X"]
    var text = "", text2 = "", text3 = "", text4 = "", text5 = ""
    let aButton = "A", bButton = "B", zButton = "Z", xButton = "X"
    var x = 0


    override func viewDidLoad() {
        super.viewDidLoad()

        createRandomLetter(text, aSecondLetter: text2, aThirdLetter: text3, aFourthLetter: text4, aFifthLetter: text5)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonA(sender: UIButton) {

        if aButton == label_1.text {
            label_1.hidden = true
            label_1.tag += 1
        }

        else {
            //play animation
            print("play animation")
        }
    }

    @IBAction func buttonB(sender: UIButton) {
        if bButton == label_1.text {
            label_1.hidden = true
        }
    }

    @IBAction func buttonX(sender: UIButton) {
        if xButton == label_1.text {
            label_1.hidden = true
        }
    }

    @IBAction func buttonZ(sender: UIButton) {
        if zButton == label_1.text {
            label_1.hidden = true
        }
    }



    func createRandomLetter(individualLetter: String, aSecondLetter: String, aThirdLetter: String, aFourthLetter: String, aFifthLetter: String) {

        let individuaLetter = visibleLetters[Int(arc4random_uniform(UInt32(visibleLetters.count)))],
        aSecondLetter = visibleLetters[Int(arc4random_uniform(UInt32(visibleLetters.count)))],
        aThirdLetter = visibleLetters[Int(arc4random_uniform(UInt32(visibleLetters.count)))],
        aFourthLetter = visibleLetters[Int(arc4random_uniform(UInt32(visibleLetters.count)))],
        aFifthLetter = visibleLetters[Int(arc4random_uniform(UInt32(visibleLetters.count)))]

        label_1.text = individuaLetter
        label_2.text = aSecondLetter
        label_3.text = aThirdLetter
        label_4.text = aFourthLetter
        label_5.text = aFifthLetter
    }

    func isCorrect() {

        if aButton == label_1.text {
            label_1.hidden = true
            label_1.tag += 1
        }

        else if label_1.tag == 1 && aButton == label_2.text {

        }
        else {
            //play animation
            print("play animation")
        }
    }
}

OK trying to figure it out what you are trying to do let's do an example. So you have 4 labels, and 2 Buttons (A,B), the labels have random generated values of A,B when you click a button you need to check if that button has the same text of the label then if it is correct we do the same with label2 if it's not correct we keep trying.

A logical way to do this can be to associate tags to your labels (that i think you are alredy doing it) and have a temporal variable to keep track of the current label for example

  var temporal:String!
  var current_tag:Int = 1
  override func viewDidLoad() {
    super.viewDidLoad()

    createRandomLetter(text, aSecondLetter: text2,
    aThirdLetter: text3, aFourthLetter: text4, aFifthLetter: text5)
     temporal = label1.text

}

  @IBAction func buttonZ(sender: UIButton) {
    //Answer is correct 
    if zButton == temporal {
        current_tag++
        IsCorrect(current_tag)
    }

}
func Iscorrect(tag:Int)
{

   if(label2.tag == tag)
   {
        temporal = label2.text
   }
   else if(label3.tag == tag)
   {
        temporal = label2.text
   }
   else if(label4.tag == tag)
   {
        temporal = label2.text
   }
}

Somethign like that should work, I didn't try it as I'm answering from my Ipad and I do not have access to my laptop but something like that should totally work, any doubt I can help you XD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM