【问题标题】:How can I remove old images of uibutton with new images in objective-c如何在objective-c中使用新图像删除uibutton的旧图像
【发布时间】:2011-04-13 13:27:03
【问题描述】:

我在编码时遇到了一个问题,那就是。我使用带有循环的自定义按钮在视图中显示了 20 张图像。一段时间后,我正在用新图像更改按钮图像。但是这里发生的事情是旧图像显示在当前图像的后面。

for(currentRow = 0; currentRow < 5; currentRow++)
    {
        for (currentColumn = 0 ; currentColumn < 4; currentColumn++)
        {
            if(indexValue<20)
            {
                printf("\n index value %d",indexValue);
                A *aObject = [aList objectAtIndex:indexValue];

                MyCustomButton *myButton = [[MyCustomButton alloc] initWithIdValue:aObject.aName];
                CGRect imageFrame = CGRectMake(currentColumn * 80+5, currentRow * 80+5, 67, 67);
                [myButton setImage:[UIImage imageNamed:aObject.aName] forState:UIControlStateNormal];
                [myButton setFrame:imageFrame];
                myButton.backgroundColor=[UIColor clearColor];
                [myButton addTarget:self  action:@selector(aAction:) forControlEvents:UIControlEventTouchUpInside];
                [myView addSubview:myButton];
                [myButton release];
                 myButton = nil;
}
}
}

帮我解决这个问题,

谢谢, 马丹·莫汉。

【问题讨论】:

  • 如果你粘贴你的代码,我相信我们会很快解决这个问题
  • 请贴一些代码,没有它很难说会发生什么。话虽如此,听起来您正在分配新的 UIImageViews 而不是更改旧的图像。
  • 编辑问题并添加最初更新 UIButton 图像的循环。
  • 我添加了代码,请参考
  • 您似乎是在创建新的按钮集,而不是更改现有按钮的图像?

标签: objective-c cocoa-touch uiimage uibutton


【解决方案1】:

正如onnoweb 已经提到的: 每次要更改图像时,不应添加新按钮。 我会选择的方法是,在视图的 viewDidLoad 中创建您需要的所有按钮并将它们添加到那里。 例如像这样:(伪代码)

- (void)viewDidLoad
{
    for(int i = 0; i < columnCount; i++)
    {
        for(int j = 0; j < rowCount; j++)
        {

            MyCustomButton *myButton = [[MyCustomButton alloc] initWithIdValue:aObject.aName];
            CGRect imageFrame = CGRectMake(currentColumn * 80+5, currentRow * 80+5, 67, 67);
            forState:UIControlStateNormal];
            [myButton setFrame:imageFrame];
            myButton.backgroundColor=[UIColor clearColor];
            [myButton addTarget:self  action:@selector(aAction:) forControlEvents:UIControlEventTouchUpInside];
            [myView addSubview:myButton];
            [myButton release];
            myButton = nil;
        }
    }
}

然后您可以使用以下几行更改循环中的图像:(伪代码)

            myButton = buttonMatrix[row][column];
            [myButton setImage:[UIImage imageNamed:aObject.aName] forState:UIControlStateNormal];

这也比总是创建新按钮而不释放旧按钮消耗的内存要少得多。

【讨论】:

  • @Madan Mohan 我在此处编写的代码不适合您将其复制并粘贴到您的项目中。那样就行不通了。我只是使用您发布的代码以伪代码编写了此代码,以使您考虑自己编写的代码。这样你就不会再犯第二次错误了。 :)
【解决方案2】:

您有两种选择来解决问题:

1) 保存您在循环中创建的按钮指针,在更新图像时,使用按钮指针再次调用 setImage:forState:,这会将按钮中的旧图像替换为新图像。并且您节省了按钮的初始化/销毁时间。

2)将按钮指针保存为步骤1),调用[button removeFromSuperView]从超级视图中删除按钮,然后重新创建一个新按钮并再次插入超级视图。不推荐这种方式,因为旧按钮将被销毁,新按钮将被初始化,这两个操作都会消耗性能(时间),并且如果您在按钮顶部添加了一些其他子视图,则在创建新按钮后,您需要使按钮顶部的所有视图再次位于顶部。因此,如果您没有任何特定原因需要重新创建新按钮,只需按照步骤 1) 进行操作,它既简单又快速。

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多