UIButton背景图像不变

问题描述:

我想根据Web服务响应更改按钮背景图像。如果作为响应,我会将未读消息变为true,然后图像将会不同,如果没有未读消息,图像将会不同。我得到的反应非常好,但无法更改我的按钮的背景图像。UIButton背景图像不变

这是我使用

if(unread>0){ 
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; 
} 
else{ 
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal]; 
} 
+4

您的按钮应该是“自定义按钮”。 – Bhavin

+2

您是否尝试过调试以查看未读值假定的值?也许问题在于它没有你期望的价值。更多的上下文,显示更多的源代码将有所帮助。 –

+0

@RamyAlZuhouri我已经调试过它,它是正确的条件,但不能改变图像。不知道为什么 – KDeogharkar

解决方案可以非常简单,如果你看看它。 保留全局UIButton而不是本地UIButton。这有助于,当Web请求返回响应时,该按钮需要在范围内存在,并且不会从内存中移除。

因此,使用

self.myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

[self.myButton setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; 

OR

[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]; 
} 

这将更改背景图像,以响应该功能的按钮。

+0

,这没有任何意义,我调试了很多时间,如果按钮被删除或不是,但它一直在那里。 – KDeogharkar

+0

该解决方案明确指出,您需要清楚,至于WHAT按钮是否响应该方法。这可以通过以下方式使用:将发送者对象类型转换为UIButton,然后将图像更改为它。试试这个,并告诉如果它不起作用 – Hitesh

+0

我自己用过这个。告诉你,它会起作用。去尝试一下 ! – Hitesh

首先检查您的按钮:)

的IBOutlet中连接并获得来自web服务的数据后设置您的代码片断得到的unread

值后,即
if(unread>0){ 
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; 
} 
else{ 
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal]; 
} 
+0

它与UIControlStateNormal在“或”中。并顺便说一句,只是UIControlStateNormal它不起作用 – KDeogharkar

+0

检查您的按钮的IBOutlet连接:) – Rajneesh071

+0

是的男人我已经绑定它 – KDeogharkar

[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal|UIControlStateSelected]; 

要补充的按钮图像UIControlStateNormalUIControlStateSelected

尝试

[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; 

发现图像是否存在,并加入适当的包和名称是否正确

+0

只有UIControlStateNormal它不起作用 – KDeogharkar

+0

图像存在@Lithu – KDeogharkar

+0

@BaZinga尝试设置图像的两行控制状态正常和选择 –

[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"); 
     } 

请尝试以下

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]; 
} 
+0

他想改变它的UIControlState选择和正常。您应该调用该方法两次以添加这两个控件状态的图像。此外,你应该给这个代码的一些解释和用户做错了什么。 –

+0

只有UIControlStateNormal它不工作 – KDeogharkar

+0

都 – KDeogharkar

设置背景图片为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]; 
} 
+0

正确,但请至少添加一些解释。 –

+0

与上面贴出的答案一样! – iphonemaclover

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]; 

我希望这适用于你....

+0

不会帮助 – KDeogharkar

+0

试试这个, if(unread.text.length> 0){badgeTitle setBackgroundImage:[UIImage imageNamed:@“unread.png”] forState:UIControlStateNormal]; } else { [badgeTitle setBackgroundImage:[UIImage imageNamed:@“read.png”] forState:UIControlStateNormal]; } 如果未读是字符串,那么这是工程... –

设置背景图片后

[badgeTitle setNeedsLayout];