当滚动超过列表的开头或结尾时,UIPickerView 使应用程序崩溃
我是 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
}
I'm new to iOS dev, so this is probably easy to fix. I have a custom view controller in which I'm adopting the protocols to control a UIPickerView in a nib. Everything works fine unless, in the iPad simulator, I scroll the picker beyond the first item in the list or the last item in the list and release. It kicks the following error:
Thread 1: Program received signal: "EXC_BAD_ACCESS"
on this line of my main.m class:
int retVal = UIApplicationMain(argc, argv, nil, nil);
Relevant code follows:
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
birdColors = [[NSArray alloc] initWithObjects:@"蓝色",@"黄色",@"红色",nil];
而不是
birdColors = [NSArray arrayWithObjects:@"Blue",@"Yellow",@"Red",nil];
Try:
birdColors = [[NSArray alloc] initWithObjects:@"Blue",@"Yellow",@"Red",nil];
instead of
birdColors = [NSArray arrayWithObjects:@"Blue",@"Yellow",@"Red",nil];
将birdColors也设置为一个属性(非原子,保留),就像您对pickerView所做的那样。
您的数组没有被保留,因此您正在访问僵尸内存。
在可执行文件的属性/常规面板中设置 NSZombieEnabled=YES。这将准确地告诉您正在访问的内容。
Make birdColors a property also (nonatomic, retain) like you do with the pickerView.
Your array is not being retained, so you're accessing zombie memory.
Set NSZombieEnabled=YES in the properties/General panel of your Executable. That will tell you exactly what is being accessed.