I have 3 buttons which are provided to the user to select which category they wish to see.
They are defined as the below:
//outlets
@IBOutlet weak var ChineseButton: UIButton!
@IBOutlet weak var IndianButton: UIButton!
@IBOutlet weak var MexicanButton: UIButton!
I have the below actions on these buttons (changing the original image button to a greyed out version):
//actions
@IBAction func ChineseButtonPressed(sender: UIButton)
{
ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Normal)
ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Selected)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Normal)
}
@IBAction func EightFacedButtonPressed(sender: UIButton) {
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Normal)
}
@IBAction func TweleveFacedButtonPressed(sender: UIButton) {
MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
}
This action function also determines the UIControlState, i have another button which, once clicked will take you to another page but needed to understand which button was selected.
I tried the below however it doesn't seem to recognise which button was selected?
@IBAction func GenerateButton(sender: UIButton) {
if ChineseButton.selected == true{
println("Chinese button was selected")
}
I am new to swift so any help would be appreciated!
Thanks :)
You are not selecting the button, you are just changing the images.
First, you can simplify all your code and set the images once, greyed out for normal, and colored for selected state:
override func viewDidLoad() {
super.viewDidLoad()
MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)
ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Selected)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Selected)
}
You can configure this on Storyboard too changing Image
at the different State Configs
of the icon:
Then, on each action you can simply select the button, and the correct image will be shown (you can use the same action for all buttons):
@IBAction func AnyButtonPressed(sender: UIButton) {
// Deselect all buttons but the pressed one
for button in [ChineseButton, IndianButton, MexicanButton] {
button.selected = sender === button
}
}