是否有自动发布的setTitle(UIButton类)?

use*_*607 0 iphone objective-c uibutton retain

众所周知,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 ++;
        }
    }
Run Code Online (Sandbox Code Playgroud)

由于按钮标题将多次更新,因此将多次调用此部分代码.我想在设置标题之前,我应该这样做:

[currentCardButton titleForState:UIControlStateNormal release]
Run Code Online (Sandbox Code Playgroud)

为了释放将不再使用的字符串(titleForState返回指向NSString的指针).

这是避免设备内存加载未使用的字符串的正确方法吗?

非常感谢,Apple92

Noa*_*oon 6

任何保留其某个属性上的值集的类也负责在该属性的值再次更改时释放旧值.别担心.