按钮中的图像不更新(iPhone)

时间:2012-10-02 14:50:41

标签: iphone ios xcode uibutton uiimage

我正在开发一个适用于iOS的应用程序(仍然在学习),我有一个类似于组合框的按钮:单击时会打开一个带有表视图的新场景,显示单击时返回的元素上一个场景并调用 updatePartner:方法。直到这一点,它工作得很好。但是,当我尝试更新按钮的图像时,它只是不更新​​它...

- (void) updatePartner:(NSInteger)index
{
NSLog(@"updatePartner: %i", index);

NSString *partnerImage = @"";
UIImage *image = @"";
NSString *text = @"";

switch (index)
{
    case 0:
        partnerImage = @"ic_pcmedic_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 1:
        partnerImage = @"ic_sapo_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 2:
        partnerImage = @"ic_telepac_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 3:
        partnerImage = @"ic_meo_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 4:
        partnerImage = @"ic_zon_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 5:
        partnerImage = @"ic_caboviaso_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 6:
        partnerImage = @"ic_artelecom_h45.png";
        image = [UIImage imageNamed:partnerImage];
        break;

    case 7:
        text = @"Ainda não sou cliente";
        break;

    default:
        break;
}

if (index <= 6)
    [[self partnerButton] setImage:image forState:UIControlStateNormal];
else
    [[self partnerButton] setTitle:text forState:UIControlStateNormal];

[[self partnerButton] setNeedsDisplay];
[[self view] setNeedsDisplay];
}

任何帮助都会被贬低!

更新:我已经解决了这个问题,但仍然没有弄清楚....我已经在同一类 updatePartner的 viewDidLoad:方法中测试了该方法: 并且它有效,当我从另一个类调用它时它不起作用...请帮助!谢谢!

此致 Ricardo Amendoeira

2 个答案:

答案 0 :(得分:0)

您是否为.h文件中的图片制作了IBOutlets?

那么你可以做到这一点:

yourimagesetoutlet.image = [UIImage imageNamed: @"imagename.png"];

尝试使用此!!这可能会解决你的问题

制作类似

的内容
switch (index)
{
    case 0:
        yourimagesetoutlet.image = [UIImage imageNamed: @"imagename.png"];

        break;
}

答案 1 :(得分:0)

你可以试试这个......

if (index <= 6) {
    [[self partnerButton] setBackgroundImage:image forState:UIControlStateNormal];
} else {
    [[self partnerButton] setTitle:text forState:UIControlStateNormal];
}