How can I remove old images of uibutton with new images in objective-c
I have got one problem while coding, that is. I displayed the 20 images in the view using my custom button with a loop. After some time I am changing the button images with new images. But what happening here the older images are displayed at the back of current images.
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];
MyCust开发者_Go百科omButton *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;
}
}
}
help me out of this,
Thank you, Madan Mohan.
As onnoweb already mentioned: You should not add a new Button everytime you want to change the image. The Approach i would choose is, to create all the buttons you need in the viewDidLoad of the view and add them there. for example like this: (pseudo-code)
- (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;
}
}
}
Then you can just change the image in the loop with the following lines: (pseudo-code)
myButton = buttonMatrix[row][column];
[myButton setImage:[UIImage imageNamed:aObject.aName] forState:UIControlStateNormal];
This is also much less memorycomsuming than always creating new buttons without releasing the old ones.
You have two options to resolve the problem:
1) save the button pointers which you created in the loop, when updating image, use the button pointer to call setImage:forState: again, this will replace the old image in the button with new image. And you save the init/destroy time of buttons.
2) save the button pointers as step 1), call [button removeFromSuperView] to remove the button from super view and then re-create a new button and insert to super view again. This is not recommended way because old buttons will be destroyed and new buttons will be initialized, both these actions cost performance(time), and if you have some other sub-views added on the top of the button, after you created the new button, you need to make all the views on the top of buttons to be on the top again. So if you don't have any specific reason that you need to re-create a new button, just follow step 1), it's simple and fast.
精彩评论