相當與Objective-C的新的,但設法到目前爲止從頭創建一個卡的應用程序。 遊戲的思想是針對PC玩一個紙牌遊戲。消息發送到釋放實例
在功能updatePlayerCardView我拿到小組第一的玩家已經卡和地方按鈕同組的名稱在屏幕上。 這一次工作得很好,但是當函數被調用第二次就失敗:
- [CFString字符串挽留]:發送到釋放實例0x604ac60
一些調試和研究後的消息我計算過,在發生這種情況的行是:
[cardButton的setTitle:[的NSString stringWithFormat:@ 「%@(%I)」,[基組名],[播放器getCardGroupCount:組]] forState:UIControlStateNormal];
所以NSString的被釋放明顯,事情是我不明白的是,所有按鈕都應該重新創建,那麼爲什麼我要有一個釋放的實例? 是否有更好的方式來每次更新播放器視圖。從一開始就不知道按鈕的數量。
-(void)updatePlayerCardView {
NSMutableArray *groups = [[[game players] objectAtIndex:0] getCardGroups];
Player *player = [game getPlayer:0];
int x = 13;
int y = 157;
for(int i = 0; i < [groups count]; i++) {
QuartetGroup *group = [groups objectAtIndex:i];
int cardGroupCount = [player getCardGroupCount:group];
if(cardGroupCount == [[game quartet] getCardsPerGroupCount]) {
}else{
UIButton *cardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cardButton.frame = CGRectMake(x, y, 140, 37);
cardButton.tag = i;
[cardButton setTitle:[NSString stringWithFormat:@"%@ (%i)", [group groupName],[player getCardGroupCount:group]]
forState:UIControlStateNormal];
[cardButton addTarget:self action:@selector(playerCardClicked:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:cardButton];
//NSLog(@"Card group added to view %@", [group groupName]);
if(i == 2 || i == 5 || i == 8 || i == 11) {
x += 157;
y = 157;
}else{
y += 45;
}
}
}
}
groupName在類QuartetGroup中設置: – Johan 2011-01-27 15:11:17