简体   繁体   English

UIPickerView问题

[英]UIPickerView problems

I have a very simple application that use a 2 component UIPickerView that causes me a crash every time I click over it. 我有一个非常简单的应用程序,它使用2个组件的UIPickerView,每次单击它都会导致崩溃。 I dragged it into my view by IB, then hooked up dataSource and delegate to File's Owner. 我通过IB将其拖动到视图中,然后连接了dataSource并委托给File的Owner。 In the .h file: 在.h文件中:

@interface SettingsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

While in .m 在.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]);
}

I dunno why it continues to give me SIGBART or EXC_BAD_ACCESS... I don't know where I'm doing wrong. 我不知道为什么它继续给我SIGBART或EXC_BAD_ACCESS ...我不知道我在哪里做错了。

Suggestions? 建议?

Thanks folks. 谢谢大家。

It's difficult to answer properly without seeing some more code. 如果没有看到更多代码,很难正确回答。 When it crashes, you should be able to see exactly what line is causing the crash (look at the call stack on the left). 当它崩溃时,您应该能够准确看到导致崩溃的行(请查看左侧的调用堆栈)。 My guess is that either one of your arrays (tipiDado or numeroDadi) are not retained properly or else that the objects stored in them are not of type NSString. 我的猜测是您的数组之一(tipiDado或numeroDadi)未正确保留,否则存储在其中的对象不是NSString类型。

If you update the question with the code you use to initialize them, it will be easier to point out the exact problem. 如果使用用于初始化问题的代码更新问题,将更容易指出确切的问题。

It's probably because your arrays (tipiDado and numeroDadi) are no longer valid. 这可能是因为您的数组(tipiDado和numeroDadi)不再有效。 Maybe they are set up as autoreleased objects? 也许它们被设置为自动发布对象?

What you can do, is start in debug mode (debug configuration and with debugger attached) and it should stop right at the line where it crashes. 您可以做的是在调试模式下启动(调试配置连接调试器),它应该在崩溃的那一行停止。

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

In didSelectRow, you are using the same row value to access both arrays. 在didSelectRow中,您使用相同的行值来访问两个数组。 If the arrays are not the same size, you could be accessing an out-of-range item. 如果阵列的大小不同,则可能是访问了超出范围的项目。

You should either check one array only based on the component parameter or to show both selections you can do this instead: 您应该只根据component参数检查一个数组,或者显示两个选择,您可以改为执行以下操作:

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

I used property/synthesize to initialize them, so when I filled them up with [NSArray arrayWithObjects:...] I haven't added retain but I forget to use self. 我使用property / synthesize来初始化它们,所以当我用[NSArray arrayWithObjects:...]填充它们时,我没有添加保留,但忘记了使用self。 notation!! 符号!!

Writing down: 写下来:

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

fixed up he problem. 解决他的问题。

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

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