Hello,
As everyone knows, setTitle automatically retains the string passed as a parameter. When the button caption needs to be changed, I guess that it is necesary to release the current (old) string before setting the new one. I wonder what is the most elegant way to dot it.
See my code sample (here, getPlayerHandFromGame method produces autoreleased strings which are retained when setTitle is invoked):
colourString = [pGame getPlayerHandFromGame:1 withColour:COLOUR_HEARTS];
// Split colourString into array of strings if not null.
if ([colourString length] != 0) {
listCards = [colourString componentsSeparatedByString:@" "];
for (cardCounterSameColour = 1; cardCounterSameColour <= [listCards count]; cardCounterSameColour ++) {
currentCardButton = [self buttonCardNumber:cardCounter];
// Objects are numbered from 0 in the array
[currentCardButton setTitle:[listCards objectAtIndex:cardCounterSameColour-1] forState:UIControlStateNormal];
cardCounter ++;
}
}
This portion of code will be called several times since the button caption will be updated several times. I guess that before setting the title, I should do something like this:
[currentCardButton titleForState:UIControlStateNormal release]
in order to release the string which will not be used anymore (titleForState returns a pointer to the NSString).
Is that the right way to avoid that the device memory gets loaded with unused strings ?
Many thanks, Apple92