jak*_*115 4 uibutton uiimage ios
我在xcode中找到了一个名为imageForState的属性,但是无法让它做我想做的事.按下按钮时,我想根据按钮的图像执行一段代码.
- (IBAction)favButton:(UIButton *)sender {
NSString *currentImage = [sender imageForState:UIControlStateNormal];
if([currentImage isEqualToString:@"already_fav"])
{
// execute code
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
Incompatible pointer types initializing NSString _strong with an expression type of UIImage
Run Code Online (Sandbox Code Playgroud)
有人可以告诉你怎么解决这个问题?
我确信你无法将字符串与图像进行比较.但是有一个解决方案.您所要做的就是为要比较的图像设置标记,并设置要比较的图像的标记.
image1.tag = 1;
image.tag =1;
if(image1.tag == image.tag) {
// execute code
}
Run Code Online (Sandbox Code Playgroud)
应该有所帮助,我希望它能做到.
所以,对于这个练习,我会告诉你.将NSString更改为UIImage
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
currentImage.tag = 1;
wantedImage.tag = 1;
if(currentImage.tag == wantedImage.tag) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助你:)
小智 5
创建 UIImage 时,它只是一个图像,没有附加字符串值。UIImage 对象不携带该名称。
如果是我(这不是唯一的解决方案,只是另一个建议),我会创建单独的 UIImage 对象(取决于我们在这里谈论的数量),因为这应该是可检查的,而字符串不是。
例如:
UIImage *image1 = [UIImage imageNamed:@"randomName"];
UIImage *image2 = [UIImage imageNamed:@"anotherRandomName"];
[myButton setImage:image2 forState:UIControlStateNormal];
if ([myButton imageForState:UIControlStateNormal] == image1) {
NSLog(@"The button shows image 1 for normal state");
}
else if ([myButton imageForState:UIControlStateNormal] == image2) {
NSLog(@"The button shows image 2 for normal state");
}
else {
NSLog(@"Error!!!!!!! :D");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11130 次 |
| 最近记录: |