【问题标题】:UIPickerView NSInvalidArgumentException', reason: '-[__NSCFString superview]: unrecognized selector sent to instanceUIPickerView NSInvalidArgumentException',原因:'-[__NSCFString superview]:无法识别的选择器发送到实例
【发布时间】:2013-03-08 20:47:25
【问题描述】:

我是 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];
}

}

【问题讨论】:

    标签: ios objective-c xcode4.2 uipickerview


    【解决方案1】:

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

    【讨论】:

    • 太棒了!你在钱上是对的。非常感谢!我已经发布了我的更新代码,它运行良好!
    【解决方案2】:

    该错误意味着您正在传递一个基于 NSString 的类,而您应该传递一个基于 UIView 的类。

    我在这里看不到任何暗示实际发生的事情,所以我希望它是return [arrayStates objectAtIndex:row]。你需要返回一个 UIView,所以除非arrayStates 只包含 UIView,这是你崩溃的基础。修复它的方式和位置可能会更复杂一些。

    【讨论】:

    • 这个答案非常有帮助!感谢您的意见!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2015-02-27
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多