IssamTP
IssamTP

Reputation: 2440

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

Upvotes: 1

Views: 1534

Answers (5)

IssamTP
IssamTP

Reputation: 2440

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.

Upvotes: 0

user467105
user467105

Reputation:

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]);

Upvotes: 0

Saa
Saa

Reputation: 1615

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

Upvotes: 0

Denis Hennessy
Denis Hennessy

Reputation: 7463

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.

Upvotes: 2

Eiko
Eiko

Reputation: 25632

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.

Upvotes: 1

Related Questions