ios 如何更改按钮上的图像设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6211878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change image set on Button
提问by Rahul
I want to change the button image on buttons click event. Here is what i am trying.
我想更改按钮单击事件上的按钮图像。这是我正在尝试的。
-(IBAction)editObjectImage:(id)sender
{
if (editButtonState == NO)
{
[editButton setImage:nil forState:UIControlStateNormal];
[editButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
}
else
{
[editButton setImage:nil forState:UIControlStateNormal];
[editButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
}
}
But my Button image is not changing. What's wrong with code?
但是我的 Button 图像没有改变。代码有什么问题?
回答by Deepak Danduprolu
I think you aren't getting round to changing editButtonState
. Your code can be reduced to.
我认为你并没有开始改变editButtonState
. 您的代码可以简化为。
-(IBAction)editObjectImage:(id)sender
{
UIButton *theButton = (UIButton*)sender;
if (editButtonState == NO) {
[theButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
} else {
[theButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
}
editButtonState = !editButtonState;
}
回答by Lay González
This is my working code:
这是我的工作代码:
NSString *shoppingListButtonImageName = @"notepad-selected";
UIImage *slImage = [UIImage imageNamed:shoppingListButtonImageName];
//put a breakpoint here to check that slImage is not nil.
[self.shoppingListButton setImage:slImage forState:UIControlStateNormal];
回答by Mobile Developer iOS Android
On button click you have to set image like this;
单击按钮时,您必须像这样设置图像;
[editButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateHighlighted];
回答by Tatvamasi
Check if the images are added properly (your [UIImage imageNamed:@""]) is not returning nil ? , otherwise it should work properly.
检查图像是否已正确添加(您的 [UIImage imageNamed:@""])未返回 nil ?,否则它应该可以正常工作。