Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling IBAction from another method without parameter

In my program 2 functions (IBAction player.Move(UIButton) and autoMove()) are supposed to be called by turns till all of the fields (UIButtons) has been clicked. For this I've created a function play(). However, I don't know how can I put the IBAction playerMove inside of play() function, because I need no parameter here.
I've found some answers and tried self.playerMove(nil) and self.playerMove(self) but it doesn't work.

import UIKit

class ViewController: UIViewController {

@IBOutlet var cardsArray: Array<UIButton> = [] 

var randomCard = 0


override func viewDidLoad() {
    super.viewDidLoad()
    self.play()
    // Do any additional setup after loading the view, typically from a nib.
}


func play () {
    self.autoMove()
    self.playerMove(self) // <----- here is my problem

}



@IBAction func playerMove(sender: UIButton) {

    switch (sender) {

    case self.cardsArray[0]:
        self.cardPressedAll(0)

    case self.cardsArray[1]:
        self.cardPressedAll(1)

    case self.cardsArray[2]:
        self.cardPressedAll(2)

    case self.cardsArray[3]:
        self.cardPressedAll(3)

    default: break
    }

}


func cardPressedAll (cardNumber: Int) {

    self.cardsArray[cardNumber].enabled = false

    self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)

    self.cardsArray.removeAtIndex(cardNumber)

}


  func autoMove (){

    self.randomCard  = Int(arc4random_uniform(UInt32(self.cardsArray.count)))

    self.cardsArray[self.randomCard].enabled = false

    self.cardsArray[self.randomCard].setBackgroundImage(UIImage(named: "nought"), forState: UIControlState.Normal)

    self.cardsArray.removeAtIndex(self.randomCard)
}

}
like image 647
Dandy Avatar asked Dec 09 '14 13:12

Dandy


1 Answers

Either you have to call playerMove: without a button, in which case you have to declare the sender parameter as an optional. Like:

@IBAction func playerMove(sender: UIButton?) {

UIButton means that you have to pass in a button. nil is not a button, but with UIButton?, that is to say Optional<UIButton>, nil is a valid value meaning the absence of a button.

Or you have to work out which button you want to pass to playerMove: to make it do what you want. Sit down and work out what you want to have happen, and what the code needs to do in order to make that happen.

like image 50
Jesper Avatar answered Oct 13 '22 05:10

Jesper