誰もが知っているように、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