我是iOS开发人员的新手,所以这可能很容易修复。我有一个自定义视图控制器,我正在采用协议来控制笔尖中的UIPickerView。一切正常,除非在iPad模拟器中,我将选择器滚动到列表中的第一项或列表中的最后一项并释放。它引发了以下错误:
线程1:编程接收信号:“EXC_BAD_ACCESS”
在我的main.m课程的这一行:
int retVal = UIApplicationMain(argc,argv,nil,nil);
相关代码如下:
ViewController.h
@interface BirdColorViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *birdColorPicker;
NSArray *birdColors;
}
@property (nonatomic,retain) IBOutlet UIPickerView *birdColorPicker;
Viewcontroller.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
}
答案 0 :(得分:2)
尝试:
birdColors = [[NSArray alloc] initWithObjects:@"Blue",@"Yellow",@"Red",nil];
而不是birdColors = [NSArray arrayWithObjects:@"Blue",@"Yellow",@"Red",nil];
答案 1 :(得分:0)
将birdColors设置为(非原子,保留)属性,就像使用pickerView一样。 您的阵列未被保留,因此您正在访问僵尸内存。 在可执行文件的属性/常规面板中设置NSZombieEnabled = YES。这将告诉你究竟正在访问什么。