views:

234

answers:

4

Tearing my hair out trying to get the most simple of tutorials to run, from the book 'Head First iPhone Development'

Have a UIPickerView which is bound to an array of objects. That is all. It doesn't do anything, all I want it to do (for now) is to display whats in the array. I've followed the steps in the book word for word, and restarted fresh at least 5 times over. On my latest attempt I've discovered how to debug and added some breakpoints.

This is as far as the app gets

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
}

if I change that line to return 0; the app loads up with an empty UIPickerView. If it's one, the app bombs out when run.

I've verified the array exists and has data - with pickerview set to 0 components I can return any row off the array to a label. It's only when I wire up the pickerview to the datasource / delegate through the IB that it bombs out.

Really at the end of my tether with this, have been trying to get it to work on and off for weeks and about ready to throw the macbook out the window and hunt down Steve Jobs with a crossbow. I feel especially stupid that it's the first example in the book. Any help would be much appreciated.

UPDATE:

Here's the code

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *) pickerViewnumberOfRowsInComponent :
  (NSInteger)component {
    return [activities count];
}


- (NSString *)pickerView:(UIPickerView *) titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [activities objectAtIndex:row];
}

I've just tried inserting a space in 'pickerViewnumberOfRowsInComponent', although there is no space in the book. Putting a space in (eg pickerView numberOfRowsInComponent) makes the UIPickerView work, but it's full of question marks instead of the array contents. It does seem like the book has a typo.

A: 

You want to add this:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [activities objectAtIndex: row];
}
Olie
thanks, thats in there already though - see the updated post with code.
roryok
A: 

You need a space between pickerView and numberOfRowsInComponent in

(NSInteger)pickerView:(UIPickerView *) pickerViewnumberOfRowsInComponent : (NSInteger)component

Unfortunately this is a typo in the book but this (along with other errors) are captured on the Head First iPhone Development Errata page (http://oreilly.com/catalog/errata.csp?isbn=9780596803544). Sorry for the headache and hope you're enjoying the book. -- Dan

Dan Pilone
Thanks dan, I got it sorted. The space issue was the main culprit, the question marks were a simple enough fix. I can't actually remember now what the issue was but it didnt take long to sort out. Thanks!
roryok
A: 

Just saw your update - glad you found the space issue. As for the question marks, that (obviously) shouldn't be happening. Can you post the code where you setup the activities array? -- Dan

Dan Pilone
+1  A: 

Just to clarify for everyone based off your original code...

Your delegate method missing space:

- (NSInteger)pickerView:(UIPickerView *) pickerViewnumberOfRowsInComponent: ...

should be:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: ...

And your other delegate method left out argument name pickerView:

- (NSString *)pickerView:(UIPickerView *) titleForRow: ...

should be:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow: ...
Richard