UIPickerView NSInvalidArgumentException',原因:' - [__ NSCFString superview]:无法识别的选择器发送到实例

时间:2013-03-08 20:47:25

标签: ios objective-c xcode4.2 uipickerview

我是Objective C和Xcode的新手,我正在尝试一步一步学习。几天来,我一直在特定问题上摸不着头脑,并且很难找到解决方案。我正在制作一个UIPickerVIew应用程序,它使用字典来填充选择器。 arrayStates是一个进入左侧组件的状态,而arrayCities是处于该状态的城市,其中是State的键。城市是正确的组成部分。我认为这个问题缩小到一个代码块。这是当我用它崩溃的值填充选择器时。我可以注释掉这段代码,它会运行得很好(只有问号作为选择器中的文本)。我收到这个错误:

NSInvalidArgumentException', reason: '-[__NSCFString superview]: unrecognized selector sent to instance

以下是我的.m文件中导致错误的代码块:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:    (NSInteger)component reusingView:(UIView *)view {

if (component == 0) {
   return [arrayStates objectAtIndex:row];
}
else {
UILabel *lblCity=[[UILabel alloc] initWithFrame:CGRectMake(5,0,220,50)];
lblCity.text= [arrayCities objectAtIndex:row];
lblCity.backgroundColor = [UIColor clearColor];
lblCity.font = [UIFont boldSystemFontOfSize:18];
return lblCity;
}

}

以下是我将代码更改为的内容,并且运行良好!感谢您快速准确的回复!!

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

if (component == 0)
{
   return [arrayStates objectAtIndex:row];
}
else
{
  return [arrayCities objectAtIndex:row];
}

}

2 个答案:

答案 0 :(得分:1)

您收到此错误,因为如果组件为0,则返回一个字符串。您可能希望使用pickerView:titleForRow:forComponent:而不是viewForRow,只返回数组中的字符串,而不是制作城市的标签。

答案 1 :(得分:0)

该错误意味着您传递的是基于NSString的课程,您应该通过基于UIView的课程。

我在这里看不到任何暗示实际发生的事情,所以我期待它return [arrayStates objectAtIndex:row]。你需要一个返回UIView,除非arrayStates除了UIView之外什么都不包含,这是你崩溃的基础。你如何以及在何处修复它可能会有点复杂。