简体   繁体   English

UIPickerView NSInvalidArgumentException',原因:“-[__ NSCFString超级视图]:无法识别的选择器已发送到实例

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

I am a rookie with Objective C and Xcode and am trying to learn one step at a time. 我是使用Objective C和Xcode的新手,并且试图一次学习一个步骤。 I have been scratching my head on a particular issue for days and am having a very hard time finding a solution. 几天来,我一直在某个特定问题上抓挠头,并且很难找到解决方案。 I am making a UIPickerVIew app that uses a dictionary to populate the picker. 我正在制作一个UIPickerVIew应用程序,该应用程序使用字典填充选择器。 The arrayStates is a state that goes into a the left component, and the arrayCities are cities that are in that state in which are keys of State. arrayStates是进入左侧组件的状态,而arrayCities是处于该状态的城市,这些城市是State的键。 The cities go in the right component. 城市是正确的组成部分。 I think have the issue narrowed down to a block of code.. It is when I go to populate the picker with the values where it crashes. 我认为问题已缩小为代码块。.就是在我用崩溃的值填充选择器时。 I can comment out this block of code and it will run just fine (only with question marks as the text in the picker). 我可以注释掉这段代码,它将正常运行(仅在选择器中使用问号作为文本)。 I get this error: 我收到此错误:

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

Here is the block of code from my .m file causing the error: 这是导致错误的.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;
}

}

Below is what I changed my code to and it is running great! 以下是我将代码更改为的代码,它运行得很好! Thank you for the fast and accurate responses!! 感谢您快速准确的回复!!

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

{

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

}

您会收到此错误,因为如果组件为0,则返回一个字符串。您可能想使用pickerView:titleForRow:forComponent:而不是viewForRow,而只是从数组中返回该字符串,而不是为其创建标签城市。

What that error means is that you are passing an NSString based class where you should be passing a UIView based class. 该错误的意思是您要传递一个基于NSString的类,而您应该传递一个基于UIView的类。

I don't see anything here that would suggest that actually happening so I expect its this return [arrayStates objectAtIndex:row] . 我在这里看不到任何暗示实际发生的东西,因此我希望它return [arrayStates objectAtIndex:row] You need a be returning a UIView so unless arrayStates contains nothing but UIView's that is the base of your crash. 您需要返回一个UIView,因此,除非arrayStates除了UIView之外arrayStates包含任何内容,否则这是崩溃的基础。 How and where you fix it can be a bit more complicated. 修复方法和修复位置可能会更加复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'NSInvalidArgumentException',原因:'-[__ NSCFString encodeString:]:无法识别的选择器已发送到实例 - 'NSInvalidArgumentException', reason: '-[__NSCFString encodeString:]: unrecognized selector sent to instance 'NSInvalidArgumentException',原因:'-[__ NSCFString objectAtIndex:]:无法识别的选择器已发送到实例 - 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 终止应用程序-'NSInvalidArgumentException',原因:'-[NSCFString objectForKey:]:无法识别的选择器已发送到实例 - Terminating app - 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance NSInvalidArgumentException - [__ NSCFString unsignedLongLongValue]:发送到实例的无法识别的选择器 - NSInvalidArgumentException -[__NSCFString unsignedLongLongValue]: unrecognized selector sent to instance [__NSCFString superview]:无法识别的选择器已发送到实例 - [__NSCFString superview]: unrecognized selector sent to instance NSInvalidArgumentException - 无法识别的选择器发送到实例? UITextView / UIPickerView - NSInvalidArgumentException - unrecognized selector sent to instance? UITextView / UIPickerView 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:-[__ NSCFString方案]:无法识别的选择器已发送到实例 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[__NSCFString scheme]: unrecognized selector sent to instance 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFString size]:无法识别的选择器已发送到实例 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance 'NSInvalidArgumentException',原因:'-[__NSCFString allKeys]:无法识别的选择器发送到实例 0x7ae2f750' - 'NSInvalidArgumentException', reason: '-[__NSCFString allKeys]: unrecognized selector sent to instance 0x7ae2f750' iOS:“ NSInvalidArgumentException”,原因:“-[__ NSCFString sortedArrayUsingFunction:context:]:无法识别的选择器已发送到实例。 - iOS: 'NSInvalidArgumentException', reason: '-[__NSCFString sortedArrayUsingFunction:context:]: unrecognized selector sent to instance.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM