这里是代码:
- (void)addAnswerWithNumber:(NSString *)numberAnswer
{
UIButton *aButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[aButton setBackgroundImage:[UIImage imageNamed:@"roundBlue.png"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(removeAnswer:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:numberAnswer forState:UIControlStateNormal];
aButton.enabled = YES;
aButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0];
aButton.titleLabel.textColor = [UIColor whiteColor];
aButton.titleLabel.textAlignment = UITextAlignmentCenter;
[dropableZone addSubview:aButton];
[buttonList addObject:aButton];
[aButton release];
[aButton release];
if ([buttonList count] > 0) {
dropHereLabel.text = @"";
[self repositionRoundButton];
}else
{
dropHereLabel.text = @"Déposez votre ou vos Réponse(s) ici";
}
}
- (void)repositionRoundButton
{
int yPos = ((890 / 2) - (([buttonList count] * (37 + 10)) / 2));
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
for (int i = 0; i < [buttonList count]; i++) {
UIButton *aButton = [buttonList objectAtIndex:i];
aButton.frame = CGRectMake(yPos, (90 /2) - (37/2) , 37, 37);
yPos = yPos + 47;
}
[UIView commitAnimations];
}
- (void)removeAnswer:(id)sender
{
UIButton *aButton = sender;
for (int i = 0; i < [answerList count]; i++) {
AnswerView *answer = [answerList objectAtIndex:i];
if ([[aButton titleForState:UIControlStateNormal] isEqualToString:answer.number]) {
[answer.backGroundImage setImage:[UIImage imageNamed:@"qcmBlueButton.png"]];
[answer setStateToNull];
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
aButton.alpha = 0.0f;
[UIView commitAnimations];
[buttonList removeObject:aButton];
[self repositionRoundButton];
[aButton removeFromSuperview];
[aButton release];
}
我在int retVal = UIApplicationMain(argc,argv,nil,nil)上有一个EXC_BAD_ACCESS; 当我点击按钮创建时 - (void)addAnswerWithNumber:(NSString *)numberAnswer。
您是否在我的代码中发现了问题?
谢谢。
答案 0 :(得分:5)
release
中不应UIButton * aButton;
- (void)addAnswerWithNumber:(NSString *)numberAnswer
两次。
我建议您在创建retain
时删除aButton
来电并删除所有[aButton release];
,因为您的对象将为autoreleased
。
此外,您还需要从方法[aButton release];
删除任何行- (void)removeAnswer:(id)sender
。
答案 1 :(得分:2)
看起来你已经结束了 - 释放aButton
。你只需alloc
一次,所以你只需要发布一次。
启用NSZombies将有助于在将来查明此类错误。
答案 2 :(得分:0)
正如大家所说,你过度释放了按钮对象。只需删除行中的保留
即可UIButton *aButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
使其成为UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
这是一个自动释放的对象,因此您无需释放它们。因此,删除释放aButton
此外,您不拥有aButton
方法中的removeAnswer
,因此请勿释放按钮。
我建议你看一下Apple Memory Management参考