0

我不明白为什么在我的 .m 文件中调用 mineHit 方法时会出现 EXC BAD ACCESS 错误。我知道这表明按钮数组已被释放,但我不明白为什么它会被释放。

#import "basicsViewController.h"

@implementation basicsViewController
@synthesize resetGame;
@synthesize scoreLabel;
@synthesize timeLabel;
@synthesize time;
@synthesize score;

-(void)newGame{
int index=0;
int yAxis=70;
for(int y=0;y<100;y=y+10){
    int xAxis=20;
    for( int x = 1; x < 11; x++) {
        buttonArray[index] = [[UIButton alloc]init];
        buttonArray[index] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonArray[index] setTag:index];
        [buttonArray[index] addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        buttonArray[index].frame = CGRectMake(xAxis, yAxis, 26.0, 26.0);
        NSLog(@"tag:%d xAxis:%d yAxis:%d",buttonArray[index].tag,(int)buttonArray[index].frame.origin.x,(int)buttonArray[index].frame.origin.y);
        [self.view addSubview:buttonArray[index]];
        xAxis=xAxis+28;
        index=x+y;
    }
    yAxis=yAxis+28;
}

//generate bombs

for (int bombs=0;bombs<10;bombs++){

    bombArray[bombs]= (arc4random()%99);
    //TODO compare against bombArray to make sure of no duplicates
    NSLog(@"BOMB AT %d",bombArray[bombs]);

}

}



- (IBAction)resetPress:(id)sender {
[self newGame];
}


- (void)buttonClicked:(UIButton*)button
{
BOOL hit;
NSLog(@"SELECTED BUTTON:%d",button.tag);
for (int b=0;b<10;b++){
    if (button.tag==bombArray[b]){
        //BOMB HIT
        hit=YES;
        b=10;
    }
    else {
        //no bomb
        hit=NO;
    }
}
if (hit==YES){
    //if hit
    NSLog(@"HIT AT %d",button.tag);
    [self mineHit];

}
else {
    //if not hit
    NSLog(@"%d is clean",button.tag);
    [self cleanHit:button];

}
}

-(void)mineHit{

for (int d=0;d<100;d++){
    NSLog(@"%i",buttonArray[d].tag);
    buttonArray[d].enabled=NO;
    [buttonArray[d] setTitle:@"*" forState:UIControlStateDisabled];
}

}
-(void)cleanHit:(UIButton*)button{
button.enabled=NO;
[button setTitle:@"!" forState:UIControlStateDisabled];

}

- (void)viewDidLoad
{
[super viewDidLoad];
[self newGame];

}

- (void)viewDidUnload
{
[self setResetGame:nil];
[self setScoreLabel:nil];
[self setTimeLabel:nil];
[super viewDidUnload];

}
@end

这是我的 .h 文件

#import <UIKit/UIKit.h>
NSInteger bombArray[];
UIButton *buttonArray[];
@interface basicsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *resetGame;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property int time;
@property int score;
-(void)newGame;
-(void)buttonClicked:(UIButton*)button;
-(void)mineHit;
-(void)cleanHit:(UIButton*)button;

@end
4

1 回答 1

1

当我编译你的代码时,我收到四个警告。所有四个警告都是相同的,并说:

假设有一个元素的暂定数组定义

警告适用于接口 (.h) 文件中的bombArray和数组的定义。buttonArray

如果我们给这两个数组一个大小,您的-mineHit方法就可以正常工作。

将 .h 文件的开头更改为:

#import <UIKit/UIKit.h>
NSInteger bombArray[10];
UIButton *buttonArray[100];
@interface basicsViewController : UIViewController

编译器生成警告是有原因的,最好尝试让您的代码干净地编译而没有警告或错误。

更新: 虽然我们在这里,但没有理由不能在接口内移动这些数组并将它们声明为实例变量。这样做意味着数组与视图控制器的单个实例相关联。您不太可能拥有此视图控制器的多个实例,但最好现在正确执行,而不是以后被咬。

#import <UIKit/UIKit.h>

@interface basicsViewController : UIViewController {
    NSInteger bombArray[10];
    UIButton *buttonArray[100];
}

有趣的是,将声明移动到界面中会将警告变成错误。

于 2012-04-10T03:39:39.653 回答