1

我是 iOS 开发新手,所以这可能很容易解决。我有一个自定义视图控制器,我在其中采用协议来控制笔尖中的 UIPickerView。一切正常,除非在 iPad 模拟器中,我将选择器滚动到列表中的第一项或列表中的最后一项之外并释放。它踢出以下错误:

线程1:程序接收信号:“EXC_BAD_ACCESS”

在我的 main.m 类的这一行:

int retVal = UIApplicationMain(argc, argv, nil, nil);

相关代码如下:

视图控制器.h

@interface BirdColorViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *birdColorPicker;
NSArray *birdColors;
}

@property (nonatomic,retain) IBOutlet UIPickerView *birdColorPicker;

视图控制器.m

- (void)dealloc
{
[birdColorPicker release];
[super dealloc];
}

...

- (void)viewDidLoad
{
    [super viewDidLoad];
    birdColors = [NSArray arrayWithObjects:@"Blue",@"Yellow",@"Red",nil];

    birdColorPicker.delegate = self;
    birdColorPicker.dataSource = self;
}

...

#pragma mark - UIPickerViewDataSource methods

//(UIPickerView *)thePickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    return [birdColors count];
}

 #pragma mark - UIPickerViewDelegate methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    return [birdColors objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    // Set value in prefs/model
}
4

2 回答 2

2

尝试: birdColors = [[NSArray alloc] initWithObjects:@"Blue",@"Yellow",@"Red",nil]; 而不是birdColors = [NSArray arrayWithObjects:@"Blue",@"Yellow",@"Red",nil];

于 2011-03-30T23:24:30.023 回答
0

像使用 pickerView 一样,让birdColors 也成为一个属性(非原子,保留)。您的数组没有被保留,因此您正在访问僵尸内存。在可执行文件的属性/常规面板中设置 NSZombieEnabled=YES。这将准确地告诉您正在访问的内容。

于 2011-03-30T20:23:09.557 回答