[英]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.