我想根据网络服务响应更改按钮背景图片。如果作为响应我将未读消息设置为true,则图像将不同,如果没有未读消息,则图像将不同。我得到了很好的响应,但无法更改按钮的背景图像。
这是我正在使用的代码段
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
}
答案 0 :(得分:7)
如果你看一下,解决方案可以非常简单。 保留全局UIButton而不是本地UIButton。这有助于,当Web请求返回响应时,按钮需要存在于范围内,而不是从内存中删除。
所以使用,
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.myButton setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
或强>
[self.myButton setImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
并且,如果您检查点击按钮,请按照步骤:
-(IBAction)checkForMsgCount: (id)sender
{
UIButton *buttonObj = (UIButton*)sender;
[buttonObj setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
这会更改背景图像,对于响应该功能的按钮。
答案 1 :(得分:1)
[YourBtn setImage:[UIImage imageNamed:@"your.png"] forState:UIControlStateNormal];
请试试。
如果你真的在你的照片中有这些照片的话,你也可以尝试一下吗?
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [[NSString alloc] initWithFormat:@"%s/yourButton.png", [bundlePath UTF8String]];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
NSLog(@"image exists");
}
else{
NSlog(@"image Does Not Exist");
}
答案 2 :(得分:1)
请尝试以下代码
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState :UIControlStateSelected];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState :UIControlStateSelected];
}
答案 3 :(得分:0)
首先检查按钮的IBOutlet连接:)
在从Web服务获取数据后设置代码,即获取unread
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
}
答案 4 :(得分:0)
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal|UIControlStateSelected];
您是否正在添加UIControlStateNormal
或UIControlStateSelected
尝试
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
查找图像是否存在并正确添加到包中且名称是否正确
答案 5 :(得分:0)
将UIControlStateNormal和UIControlStateSelected的背景图像设置为单独的行。
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateSelected];
} else {
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateSelected];
}
答案 6 :(得分:0)
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(50, 50, 100, 50);
[btn setTitle:@"ok" forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
btn.backgroundColor=[UIColor blueColor];
我希望这适合你......
答案 7 :(得分:0)
[badgeTitle setNeedsLayout];