2016-03-03 84 views
0

我得到這個IBAction爲攪亂一副撲克牌隨機IBAction內部調用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) 
    } 

...和代碼的分數,這部分的一個點時,卡賽,然而,當他們macth我需要把它重新洗牌。我可以在IBAction中使用IBAction playRoundTapped snapButtonTapped嗎?

@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) 

      } 

    } 

回答

3

是的,你可以。一個IBAction只是一個具有特定簽名和標籤的方法,告訴Interface Builder它可以連接到控件。你可以自由地打電話給你。

+0

你如何在我的情況下調用它在snapButtonTapped裏面playRoundTapped? – Miguel

+0

@Sam'self.playRoundTapped(self.buttonReference)' – GoodSp33d

+0

我試過'self.playRoundTapped(self.playRoundButton)',它可以工作,但它只是弄亂了卡片,有沒有另一種方式可以使卡片洗牌時,他們是匹配。 – Miguel