views:

48

answers:

5

Hi all, I have a very simple application that use a 2 component UIPickerView that causes me a crash every time I click over it. I dragged it into my view by IB, then hooked up dataSource and delegate to File's Owner. In the .h file:

@interface SettingsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

While in .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.

Suggestions?

Thanks folks.

+1  A: 

It's probably because your arrays (tipiDado and numeroDadi) are no longer valid. 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.

Eiko
+1  A: 

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.

If you update the question with the code you use to initialize them, it will be easier to point out the exact problem.

Denis Hennessy
A: 

Try using Allocations with zombo detections / reference counter to see which object is the problem.

Sander Backus
A: 

In didSelectRow, you are using the same row value to access both arrays. 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:

NSInteger tipiDadoRow = [thePickerView selectedRowInComponent:0];
NSInteger numeroDadiRow = [thePickerView selectedRowInComponent:1];
NSLog(@"Selected Dice: %@. Number of Dice: %@", 
 [tipiDado objectAtIndex:tipiDadoRow], [numeroDadi objectAtIndex:numeroDadiRow]);
aBitObvious
Thank you, I've noticed it and fixed it up.
IssamTP
A: 

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. notation!!

Writing down:

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

fixed up he problem.

IssamTP