繁体   English   中英

UIPickerView问题

[英]UIPickerView problems

我有一个非常简单的应用程序,它使用2个组件的UIPickerView,每次单击它都会导致崩溃。 我通过IB将其拖动到视图中,然后连接了dataSource并委托给File的Owner。 在.h文件中:

@interface SettingsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

在.m中

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger value;

if (component == 0) {
    value = [tipiDado count];
} else {
    value = [numeroDadi count];
}

return value;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
    return [tipiDado objectAtIndex:row];
} else {
    return [numeroDadi objectAtIndex:row];
}
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
NSLog(@"Selected Dice: %@. Number of Dice: %@", [tipiDado objectAtIndex:row], [numeroDadi objectAtIndex:row]);
}

我不知道为什么它继续给我SIGBART或EXC_BAD_ACCESS ...我不知道我在哪里做错了。

建议?

谢谢大家。

如果没有看到更多代码,很难正确回答。 当它崩溃时,您应该能够准确看到导致崩溃的行(请查看左侧的调用堆栈)。 我的猜测是您的数组之一(tipiDado或numeroDadi)未正确保留,否则存储在其中的对象不是NSString类型。

如果使用用于初始化问题的代码更新问题,将更容易指出确切的问题。

这可能是因为您的数组(tipiDado和numeroDadi)不再有效。 也许它们被设置为自动发布对象?

您可以做的是在调试模式下启动(调试配置连接调试器),它应该在崩溃的那一行停止。

尝试将分配与zombo检测/参考计数器一起使用,以查看问题出在哪个对象上。

在didSelectRow中,您使用相同的行值来访问两个数组。 如果阵列的大小不同,则可能是访问了超出范围的项目。

您应该只根据component参数检查一个数组,或者显示两个选择,您可以改为执行以下操作:

NSInteger tipiDadoRow = [thePickerView selectedRowInComponent:0];
NSInteger numeroDadiRow = [thePickerView selectedRowInComponent:1];
NSLog(@"Selected Dice: %@. Number of Dice: %@", 
 [tipiDado objectAtIndex:tipiDadoRow], [numeroDadi objectAtIndex:numeroDadiRow]);

我使用property / synthesize来初始化它们,所以当我用[NSArray arrayWithObjects:...]填充它们时,我没有添加保留,但忘记了使用self。 符号!!

写下来:

self.tipiDado = [NSArray arrayWithObjects:@"D4",...];

解决他的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM