G.Venem
G.Venem

Reputation: 23

Swift: Using UIButtons as UIImages

I'm working on a dice game with Swift. I want give players the choice to hold a 'dice' for a turn when they tap on it. Because of this I want to use UIButtons as the dices. I got the dices working as UIImages and now I want to transfer the UIImages over to UIButtons. But while doing this I got an error:

Argument labels '(named:)' do not match any available overloads.

I'm kinda clueless and don't really know how to fix this.

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var dice1: UIButton!
@IBOutlet weak var dice2: UIButton!

let stenenArray = ["Steen1, Steen2, Steen3, Steen4, Steen5, Steen6"]

var randomDiceIndex1 : Int = 0
var randomDiceIndex2 : Int = 0

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func buttonPressed(_ sender: Any) {
    updateDiceImages()
}


func updateDiceImages(){
    dice1 = UIButton(named: stenenArray[randomDiceIndex1])
    dice2 = UIButton?(named: stenenArray[randomDiceIndex2])

    randomDiceIndex1 = Int(arc4random_uniform(6))
    randomDiceIndex2 = Int(arc4random_uniform(6))
}
}

Upvotes: 0

Views: 105

Answers (3)

Ahmad F
Ahmad F

Reputation: 31645

Since dice1 and dice2 are IBOutlets, I would consider that there is no need to re-initialize them.

Even though, UIButton(named: ...) seems to be weird!

So what you should do instead of -the weird initialization- for the buttons:

dice1 = UIButton(named: stenenArray[randomDiceIndex1])
dice2 = UIButton?(named: stenenArray[randomDiceIndex2])

is to simply set an image for each one, by using setImage(_:for:) method:

dice1.setImage(UIImage(named: stenenArray[randomDiceIndex1]), for: .normal)
dice2.setImage(UIImage(named: stenenArray[randomDiceIndex2]), for: .normal)

Update:

It seems that you would need to implement updateDiceImages() as:

func updateDiceImages() {
    randomDiceIndex1 = Int(arc4random_uniform(6))
    randomDiceIndex2 = Int(arc4random_uniform(6))

    dice1 = UIButton(named: stenenArray[randomDiceIndex1])
    dice2 = UIButton?(named: stenenArray[randomDiceIndex2])
}

which means that you should generate the random values for both randomDiceIndex variables before setting the UIButtons images, otherwise the first roll would be always zeros.

Upvotes: 1

DarkDust
DarkDust

Reputation: 92316

There is no initializer for UIButton using an argument named:. Guessing from your dice1 and dice2 variables being declared as @IBOutlet, you probably already have the buttons and now only want to assign the images instead of creating new buttons:

dice1.setImage(UIImage(named: stenenArray[randomDiceIndex1]), for: .normal)

Upvotes: 5

Mansi
Mansi

Reputation: 628

Replace

dice1 = UIButton(named: stenenArray[randomDiceIndex1])
dice2 = UIButton?(named: stenenArray[randomDiceIndex2]) 

with

dice1.setImage(UIImage(named: stenenArray[randomDiceIndex1]), for: .normal)
dice2.setImage(UIImage(named: stenenArray[randomDiceIndex2]), for: .normal)

Upvotes: 3

Related Questions