1

I have an array of buttons and at the beginning of the program I want the user to pick one button and then it'll change its background. I've written a switch-statement but I don't know how can I implement it into override func viewDidLoad(), namely, I have no idea what should I write as a parameter instead of UIButton

class ViewController: UIViewController {

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


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

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


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

    default: break
    }

}



func cardPressedAll (cardNumber: Int) {

    self.cardsArray[cardNumber].enabled = false

    self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
}
}
Dandy
  • 929
  • 2
  • 14
  • 23

1 Answers1

0

Instead of calling 'self.playerMove(sender: UIButton)' in your view did load you need to connect your buttons to your IBAction using storyboard.

matteok
  • 2,189
  • 3
  • 30
  • 54
  • so simple! thank you. so can you tell me what is actually viewDidLoad class? iOS Developer Library doesn't explain it for me precisely enough. – Dandy Dec 07 '14 at 19:14
  • viewDidLoad is a method in UIViewController that is fired when the viewcontrollers view has finished loading. You usually do your all of your setup in that method. – matteok Dec 07 '14 at 19:27
  • your answer helped me a lot, bu actually I have the same problem again: I don't know how to use my IBAction playerMove inside of a main function play. It should look like that: `func play () { self.playerMove(UIButton) self.autoMove() }` – Dandy Dec 07 '14 at 19:31
  • In your storyboard you can connect the UIButtons you dragged onto your view controller to IBActions in your code. Since this is very basic stuff I suggest you begin with some tutorials on swift and xcode like this one http://www.raywenderlich.com/74904/swift-tutorial-part-2-simple-ios-app. – matteok Dec 08 '14 at 10:10
  • I have connected the UIButtons with my IBAction. The problem is, that this IBAction is automatically executed after opening the program and my other function is not. It's a simple noughts and crosses game, I have two functions - `playerMove(UIButton)` (player chooses a field) and `autoMove()` (a computer chooses a field). I want those 2 functions to be executed one after another so I wanted to put them inside of `play()` function with while-statement. The issue is - how can I put and IBAction inside of function, just like simply `self.autoMove()`. I hope it's more clear now ;) – Dandy Dec 08 '14 at 14:46
  • I suppose here is an answer to my question, however, it's in Objective-C: http://stackoverflow.com/questions/2889408/whats-the-best-way-to-call-an-ibaction-from-with-in-the-code when I tried `self.playerMove(nil)` it didn't work – Dandy Dec 08 '14 at 15:11