UIButton 作为开关
我正在尝试在 iOS 版 Xcode4 中使用自定义图像创建一个类似于“按按按关”的按钮。 我正在使用的代码
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
目前工作正常。 但我的问题是,当我打开时,我按下它,然后它再次弹出,然后最后打开。 该应用程序可以运行,但确实很难看。
我首先将“突出显示”图像设置为打开。因此,当我突出显示该按钮时,它会打开并且会弹出。效果很好。但当我再次将其关闭时,问题是相同的,方向相反。
我尝试输入该代码:
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage[UIImage imageNamed@"off.png"] forState:UIControlStateHighlighted];
}
else
{
[button setImage[UIImage imageNamed@"on.png"] forState:UIControlStateHighlighted];
}
button.selected = !button.selected;
}
但只要 button.selected = !button.selected
就没有区别。 所以不会有任何改变。
我还尝试在“Touch Down”上触发 IBAction,但您可以想象这会是多么令人沮丧。
有人有解决这个问题的方法吗? 有人也为此苦苦挣扎吗?
问候,非常感谢 朱利安
I'm trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS.
The code I'm using is
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
That works fine for now.
But my problem is, that when I'm toggling on, I press it on, then it is popping off again and then finally on.
The app works, but that is really ugly, though.
I firstly set the "highlighted" image to on. So when I highlight the button, it is on and that popping to on. That works fine. But when I turn it off again, the problem is the same, in the reverse direction.
I tried to put that code:
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage[UIImage imageNamed@"off.png"] forState:UIControlStateHighlighted];
}
else
{
[button setImage[UIImage imageNamed@"on.png"] forState:UIControlStateHighlighted];
}
button.selected = !button.selected;
}
But as long button.selected = !button.selected
there is no difference.
So it won't make any change.
I also tried to trigger the IBAction on »Touch Down« but you can imagine how frustrating this will look like.
Has anybody got a solution for that problem?
Did anybody struggle with that one too?
Greets, thanks a lot
Julian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要手动切换图像,只需在 Interface Builder 中设置选定状态的图像,并在点击按钮时交换
selected
属性。Don't manually switch the images around, just set the selected state's image in Interface Builder and swap the
selected
property over when the button is tapped.我以前也遇到过类似的问题,点击按钮时有点奇怪。尝试此代码,让我知道它是否有效
当您点击并按住按钮时,状态实际上是突出显示的并显示为“突出显示”。已选择,因此您需要一个用于突出显示和选定状态的图像。
I've had a similar problem to this before, the button works a little strangely when tapping. Try this code and let me know if it works
When you tap and hold on a button the state is actually Highlighted & Selected so you need an image for both Highlight and selected state.
同意吉姆的观点,您的代码需要进行很少的修改,如下所示......
Agree with Jim, your code needs little modification as below...
创建两个按钮。
将按钮一个放在一个之上。
可以设置默认值和使用设计生成器中的自定义按钮选项选择图像。
Have two buttons created.
place buttons one above one.
Can set Default & Selected images using Custom Button option in design builder.
@Jim - 你什么时候切换 isSelected 状态?每次按下 isHighlighted 都会被调用两次。我的 UIButton 开关的破解版本如下:-
@Jim - when do u switch the isSelected state? isHighlighted gets called twice for every press. My hacked version of UIButton switch below : -