iphone - 使用函数后的 EXC_BAD_ACCESS

标签 iphone ios exc-bad-access

这里是代码:

- (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.

您在我的代码中看到问题了吗?

谢谢。

最佳答案

你不应该 release UIButton * aButton;两次 - (void)addAnswerWithNumber:(NSString *)numberAnswer .

我建议删除retain创建时调用 aButton并删除所有 [aButton release];因为你的对象将是 autoreleased .

此外,您还需要删除任何情况下的行 [aButton release];来自方法 - (void)removeAnswer:(id)sender .

关于iphone - 使用函数后的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7429946/

相关文章:

objective-c - 在 iPad 中解析 XML 文件时如何处理 CDATA 标签

ios - 解析.com includeKey : not working

objective-c - 当我更改 moviePlayer contentURL 时 EXC_BAD_ACCESS

objective-c - EXC_BAD_ACCESS EXC_I386_GPFLT 同时点击按钮

ios - EXC_BAD_ACCESS UITable 在 numberOfRowsInSection 中设置 tableHeaderView

ios - 什么是 rootViewController 以及何时使用它?

iphone - 如何从 uiimageview op uibutton 中删除图像

javascript - Dashcode 代码翻译

iphone - 如何将单元格添加到tableview

ios - SwiftUI - 设置状态栏背景颜色以与导航栏对齐