ios – 设置按钮图像方法

前端之家收集整理的这篇文章主要介绍了ios – 设置按钮图像方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望此方法检查条件是否为真,然后设置特定按钮(按钮)以获得我指定的图像.这是代码.
-(void)canAfford:(float) f: (UIButton*)button {
    if([self playerHas:(f)] == YES) {
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ButtonBGclicked.png"] forState:UIControlStateHighlighted];
    } else {
        [button setImage:[UIImage imageNamed:@"ButtonBG.png"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateHighlighted];
    }
}

这是我得到的错误.

2013-09-22 10:35:39.985 Tapple[15663:a0b] CUICatalog: Invalid asset name supplied:,or invalid scale factor: 1.000000

解决方法

你正在调用[UIImage imageNamed:@“”]或[UIImage imageNamed:nil].没有图像与空字符串匹配.如果您要清除图像,请传递nil而不是调用imageNamed:.

试试这个(更新的原始代码,包含@Kyle Fang的评论):

- (void)canAfford:(float) f: (UIButton*)button {
    if([self playerHas:(f)] == YES) {
        [button setImage:nil forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ButtonBGclicked.png"] forState:UIControlStateHighlighted];
    } else {
        [button setImage:[UIImage imageNamed:@"ButtonBG.png"] forState:UIControlStateNormal];
        [button setImage:nil forState:UIControlStateHighlighted];
    } }
原文链接:https://www.f2er.com/iOS/332935.html

猜你在找的iOS相关文章