objective-c - 错误 : 'row" undeclared UI Picker

标签 objective-c ios xcode uipickerview

我很难理解 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]];

}

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

最佳答案

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

你拥有什么:

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

它应该是什么:

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

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

还有

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

要使用位于应用程序包中的图像创建 UIImage,请使用以下语法:

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

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

关于objective-c - 错误 : 'row" undeclared UI Picker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727228/

相关文章:

objective-c - forwardInvocation : + getArgument:atIndex methods 中出现奇怪的 "zombie"

iOS,用于调整自定义 subview 中的内容属性的模式

iOS 分发证书即将到期。我有什么选择?

ios - 应用 Retina 4 外形规范按钮不起作用

ios - 如何将 PHAsset 存储到 Core Data

ios - Objective-C 下载 PLIST 并在表中使用

ios - 当连接由 iOS 8.3 供电的设备时,设备在 Xcode 6.1 中不合格

ios - UISlider:更改了高度,但最大值闪烁

xcode - 动态添加资源到 XCode 项目,总是落后一个周期

objective-c - |NSArray objectEnumerator| 内存泄漏?