Reputation: 1051
I am trying to make a Sets! game in which I need to choose 3 cards from the UI
and compare between them (I am comparing the names of the images) for example:
UIButton
1 w/ image name : "1123"
UIButton
2 w/ image name : "1223"
UIButton
3 w/ image name : "1133"
Using this func:
func checkingIfMatch(cardOne : String, cardTwo : String, cardThree : String) -> Bool {
let first = cardOne.compactMap{Int(String($0))} //making an array from the UIImage file name
let second = cardTwo.compactMap{Int(String($0))}
let third = cardThree.compactMap{Int(String($0))}
if (first[0] == second[0] && second[0] == third[0]) || (first[0] != second[0] && second[0] != third[0]) {
if (first[1] == second[1] && second[1] == third[1]) || (first[1] != second[1] && second[1] != third[1]) {
if (first[2] == second[2] && second[2] == third[2]) || (first[2] != second[2] && second[2] != third[2]) {
if (first[3] == second[3] && second[3] == third[3]) || (first[3] != second[3] && second[3] != third[3]) {
print("match!")
return true
}
}
}
}
print("no match!")
return false
}
}
I am trying to get the name of the UIButton
's image when tapping on the button in order to do it but seems like I cant find a way to do it.
Any help will be appreciated, if there is no way to do it would like to hear different ideas to do it.
Upvotes: 1
Views: 123
Reputation: 5088
I don't know what are you trying to achieve, but you can use tag
get what button was tapped.
In the Storyboard, connect your three buttons to the same @IBAction
with sender
type of UIButton
, then simply case around the tag to know what button user tapped.
@IBAction func tappedButton(_ sender: UIButton) {
switch sender.tag {
case 0: // image One
case 1: // image Two
case 2: // image Three
default: // default condition where there is no matching cases
}
}
Just drag the buttons to the function, Don't forget to give them tags.
btn.tag = 0
Or directly from the IB.
Upvotes: 1