如何更改按钮上的图像?

时间:2015-03-01 07:35:07

标签: ios objective-c iphone

我有两个左下角(<)侧面按钮和右侧(>)按钮。我必须用左右按钮动作从数组中更改图像?

代码:

- (IBAction)onClickRightLeft:(id)sender {

    switch ([sender tag]) {
        case 10:
            indexValueL++;
            if(indexValueL >= 3)
            {
                indexValueL = 2;
            }
            if(indexValueL < 0)
            {
                indexValueL=0;
            }
            [imgViewStory setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[arrGlobal objectAtIndex:indexValueL] valueForKey:@"FeaturedStoryImage"]]]];
            break;
        case 20:
            indexValueR++;
            if(indexValueR >= 3)
            {
                indexValueR = 2;
            }
            if(indexValueR < 0)
            {
                indexValueR=0;
            }
            [imgViewStory setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[arrGlobal objectAtIndex:indexValueR] valueForKey:@"FeaturedStoryImage"]]]];
            break;

        default:
            break;
    }

如何管理数组索引?

1 个答案:

答案 0 :(得分:0)

我认为这应该做..(未经测试)

- (IBAction)onClickRightLeft:(id)sender {

    switch ([sender tag]) {
        case 10: // considering right clicked
            indexValue++;
            break;
        case 20: // considering left clicked
            indexValue--;
            break;
    }

    if(indexValue >= 3)
    {
        indexValue = 0;
    }
    if(indexValue < 0)
    {
        indexValue=2;
    }
    [imgViewStory setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[arrGlobal objectAtIndex:indexValue] valueForKey:@"FeaturedStoryImage"]]]];

同时在显示图像时,您应该考虑缓存和放大图像。 asynch loading。查看this for more info