Reputation: 85
I am using 5 UIButton and I'm getting emoji string in this buttons. But the problem is, when I click the button, emoji disappear on the screen. I guess this is highlighted or selected color problem, but I didn't figure out. What should I do for this?
My code :
func buttonSelectedNot(){
button1.selected = false
button2.selected = false
button3.selected = false
button4.selected = false
button5.selected = false
}
@IBAction func favoriteButtonAction(sender: UIButton) {
// Save Data
buttonSelectedNot()
sender.selected = !sender.selected;
if (sender.selected)
{
sender.selected = true
emojiString = (sender.titleLabel?.text)!
print(emojiString)
}
else
{
print("Not Selected");
}
}
Upvotes: 1
Views: 469
Reputation: 85
I solved the problem. The problem is, You need to set titlelabel color for the button. If you don't, it's disappear.
Solved code :
func buttonSelectedNot(){
button1.selected = false
button2.selected = false
button3.selected = false
button4.selected = false
button5.selected = false
}
@IBAction func favoriteButtonAction(sender: UIButton) {
// Save Data
buttonSelectedNot()
sender.selected = !sender.selected;
if (sender.selected)
{
sender.selected = true
emojiString = (sender.titleLabel?.text)!
**//need to set title color for the button.**
sender.setTitleColor(UIColor.blackColor(), forState: .Selected)
print(emojiString)
}
else
{
print("Not Selected");
}
sender.selected = true
}
And it's look perfect :D
Upvotes: 1
Reputation: 1519
Hmm, never worked with emojistrings, but your code is a bit weird. When a button is clicked you set all buttons to selected = false
then you set the sender.selected = !sender.selected
(which means that sender.selected = true
, provided that the sender is one of the buttons in buttonsNotSelected()
). Then you check if sender
is selected and if it is selected you set it to true again and then add the emojistring.
What happens if the sender.selected = true
? Well, you will set it to false using buttonsNotSelected()
and then you will invert it and set it to true, even though it probably should be false
(provided that sender
is one of the buttons in said method). Not sure if this is any issue though, but it seems to me like the reason for the emoji to disappear is because .selected
is not set correctly.
Try to clean up your code a bit
func buttonSelectedNot(){
button1.selected = false
button2.selected = false
button3.selected = false
button4.selected = false
button5.selected = false
}
@IBAction func favoriteButtonAction(sender: UIButton) {
// Save Data
sender.selected = !sender.selected;
if (sender.selected) {
emojiString = (sender.titleLabel?.text)!
print(emojiString)
} else {
print("Not Selected");
}
// SET ALL BUTTONS, EXCEPT SENDER, TO FALSE
}
Upvotes: 1