0

我很难理解它是如何pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component工作的

我已经设置好我的选择器,让我的 NSArray 填充它,我想要做的是显示来自选定行的图像。我试过了:

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

}

但我收到一个错误,告诉我“行”未声明?这里: [imageview setImage:[arrStatus objectAtIndex:row]];

4

1 回答 1

1

看起来你的委托方法是错误的。

你有什么:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:  (NSInteger)component {

它应该是什么:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

注意(NSInteger)rowinComponent之间有一个空格。这很重要,因为 row 是局部变量的名称,而不是方法名称的一部分。

似乎您已经用字符串而不是图像填充了该数组。如果您希望拥有 [imageview setImage:..];,我建议创建两个 UIImages 并将它们放入数组中。方法正常工作。

要使用位于 Application Bundle 中的图像创建 UIImage,请使用以下语法:

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AppleStoreLogo" ofType:@"jpg"]];

**注意确保您已将图像正确添加到应用程序包中。为此,首先将图像与应用程序的文件夹放在某处。接下来单击并将该图像拖到 XCode 中的资源中。XCode 将询问您是否要将图像添加到包中,选择是。而已 :)

于 2012-07-30T18:44:34.883 回答