TouchUpInside事件后无法将UIButton保持在选定状态 [英] Unable to keep UIButton in selected state after TouchUpInside Event
问题描述
我需要一个UIButton来保持按下状态.基本上,如果一个按钮处于正常状态,我想触摸该按钮,它将其突出显示为标准的蓝色,然后在举起手指后保持蓝色.我做了以下UIAction并将按钮Touch Up Inside事件连接到它.
I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger. I made the following UIAction and connected the buttons Touch Up Inside event to it.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
但是发生了什么,当我松开手指后,按钮又回到了白色背景.为了进行测试,我添加了一个UISwitch并让它执行相同的代码:
But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
在这种情况下,按钮会像我期望的那样切换到突出显示和未突出显示的状态.
In this case the button toggles to a highlighted and non-highlighted state as I would expect.
在"Touch Up Inside"事件之后是否发生了另一个事件,该事件将状态重置为正常"?如何保持突出显示状态?
Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?
推荐答案
当您触摸/释放按钮时,iOS将应用并删除突出显示的状态.因此,不要在您的操作方法中依赖它.
The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.
如其他答案之一所述,将突出显示的图像设置为与所选图像相同,然后修改您的操作方法:
As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:
-(IBAction) emergencyButtonPress:(id) sender
{
emergencyButton.selected = !emergencyButton.selected;
}
这篇关于TouchUpInside事件后无法将UIButton保持在选定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!