众所周知,setTitle 会自动保留作为参数传递的字符串。当需要更改按钮标题时,我想在设置新字符串之前必须释放当前(旧)字符串。我想知道最优雅的点缀方式是什么。
查看我的代码示例(这里,getPlayerHandFromGame 方法生成自动释放的字符串,这些字符串在调用 setTitle 时会保留):
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 ++;
}
}
这部分代码将被多次调用,因为按钮标题将被多次更新。我想在设置标题之前,我应该这样做:
[currentCardButton titleForState:UIControlStateNormal release]
为了释放不再使用的字符串(titleForState 返回一个指向 NSString 的指针)。
这是避免设备内存加载未使用字符串的正确方法吗?
非常感谢, Apple92