我正在使用5个UIButton,我在这个按钮中获得了表情符号字符串。但问题是,当我点击按钮时,表情符号会消失在屏幕上。我猜这是突出显示或选择的颜色问题,但我没弄明白。我该怎么做?
我的代码:
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");
}
}
答案 0 :(得分:1)
嗯,从未使用过emojistrings,但你的代码有点奇怪。单击按钮后,您将所有按钮设置为selected = false
,然后设置sender.selected = !sender.selected
(这意味着sender.selected = true
,前提是发件人是buttonsNotSelected()
中的其中一个按钮。然后检查是否选择了sender
,如果选中它,则再次将其设置为true,然后添加表情符号。
如果sender.selected = true
会怎样?好吧,你将使用buttonsNotSelected()
将其设置为false,然后你将它反转并将其设置为true,即使它可能应该是false
(前提是sender
是其中一个按钮在所述方法中)。不确定这是否有任何问题,但在我看来,表情符号消失的原因是因为.selected
未正确设置。
尝试稍微清理一下代码
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
}
答案 1 :(得分:0)
我解决了这个问题。问题是,您需要为按钮设置titlelabel颜色。如果你不这样做,它就会消失。
解决的代码:
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
}
它看起来很完美:D