问题描述
当用户按下该按钮时,我想更改UIButton上的图像.所以,我写了
I want to change the image on UIButton, when User presses that button. So, I wrote
btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];
然后我改变了看法.
图像正在更改,但不显示更改后的图像.我认为控件会转到下一个视图,这就是发生这种情况的原因.但是我想显示更改,我该怎么做?请为此帮我..
The image is changing but not display the changed image.I think the controll goes to next view tha's why it is happening. But I want to show the change, How can I do this?? Plz help me for this..
推荐答案
我正在使用Interface Builder,在其中向视图添加了UIButton
.
I am using Interface Builder, in which I added a UIButton
to the view.
-
默认情况下,我定义了 selected 按钮,并为 selected 状态选择了我的图片(使用检查器"窗口中的下拉列表选择"选择图像之前,请先选择选定状态配置".
I defined the button selected by default and selected my image for the selected state (Use drop-down list in Inspector window to choose "Selected State Configuration" before selecting the image).
在控制器中创建一个IBAction
并将按钮连接到该动作.
Create an IBAction
in the controller and connect the button to that action.
然后看到下面的代码:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
我认为这是一个非常平稳的解决方案.希望对您有帮助!
I think this is quite a smooth solution. Hope it helps!
这篇关于用户在iPhone上按下按钮时更改UIButton上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!