当滚动超过列表的开头或结尾时,UIPickerView 使应用程序崩溃

发布于 2024-10-27 21:05:09 字数 1583 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-11-03 21:05:09

尝试:
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];

感受沵的脚步 2024-11-03 21:05:09

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文