【问题标题】:calling IBAction button inside IBAction?在 IBAction 中调用 IBAction 按钮?
【发布时间】:2016-03-03 23:29:58
【问题描述】:

我得到了这个随机洗牌的 IBAction

@IBAction func playRoundTapped(sender: UIButton) {

    cardNamesArray = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(cardNamesArray) as! [String]
    cardNamesArray2 = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(cardNamesArray2) as! [String]



    let firstCardString:String = self.cardNamesArray[0]
    self.FirstCardImageView.image = UIImage(named: firstCardString)

    let SecondCardString:String = self.cardNamesArray2[1]
    self.SecondCardImageView.image = UIImage(named: SecondCardString)
    }

...当卡片匹配时,这部分代码会得分,但是当它们匹配时,我需要再次洗牌。我可以在 IBAction snapButtonTapped 中使用 IBAction playRoundTapped 吗?

@IBAction func SnapButtonTapped(sender: UIButton) {


        if firstRandomNumber == SecondRandomNumber {

        print("index match")

        self.playerScore += 1
        self.playerScoreLabel.text = String(self.playerScore)

        cardNamesArray.removeAtIndex(firstRandomNumber)
        cardNamesArray2.removeAtIndex(SecondRandomNumber)

            if cardNamesArray.count == 0 && cardNamesArray2.count == 0{


                print("user won")
                //alert box
                let alert = UIAlertController(title:"Level completed", message: "Your score is \(playerScore)", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title:"next level", style: UIAlertActionStyle.Default, handler: nil))
                    self.presentViewController(alert, animated:true, completion:nil)

            }

    }

【问题讨论】:

    标签: swift random ibaction


    【解决方案1】:

    是的,你可以。 IBAction 只是一个具有特定签名和标签的方法,它告诉 Interface Builder 它可以连接到控件。您可以自己调用它。

    【讨论】:

    • 在我的情况下你会怎么称呼它 playRoundTapped inside snapButtonTapped?
    • @Sam self.playRoundTapped(self.buttonReference)
    • 我尝试了“self.playRoundTapped(self.playRoundButton)”,它可以工作,但它只是把卡片弄乱了,有没有其他方法可以让卡片在匹配时洗牌。
    • 您没有真正告诉我们有关您的应用程序的足够信息,以便我们帮助洗牌而不弄乱它们。这是一个单独的问题。
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2011-03-29
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多