Im having difficulty in understanding how the pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component works
Ive set up my picker ok, got my NSArray to populate it, what im trying to do is display an image from a selected row. I tried:
- (void)viewDidLoad
{
////arrays & objects
arrStatus = [[NSArray alloc] initWithObjects:@"Appstorelogo",@"app",nil];
//number of colums and rows etc of picker
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
//set number of rows
return arrStatus.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f orComponent:(NSInteger)component
{
//set item per row
return [arrStatus objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent: (NSInteger)component
{
[imageview setImage:[arrStatus objectAtIndex:row]];
}
but i get an error telling me “row” is undeclared? here: [imageview setImage:[arrStatus objectAtIndex:row]];
It looks as if your delegate method is wrong.
what you have:
What it should be:
Notice there is a space between (NSInteger)row and inComponent. This is important because row is the local variable’s name and not part of the method’s name.
ALSO
It seems as you have populated that array with strings, not images. I would suggest creating two UIImages and putting them in the array if you are hoping to have the [imageview setImage:..]; method working correctly.
To create a UIImage with an image located within your Application Bundle, use this syntax:
**Note Be sure you’ve added the image to your application bundle properly. To do so, first place the image with the app’s folder somewhere. Next click and drag that image over in to XCode into resources. XCode will ask you if you want to add the image to the bundle, select Yes. That’s it 🙂