簡體   English   中英

UIPickerView NSInvalidArgumentException',原因:“-[__ NSCFString超級視圖]:無法識別的選擇器已發送到實例

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

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

}

您會收到此錯誤,因為如果組件為0,則返回一個字符串。您可能想使用pickerView:titleForRow:forComponent:而不是viewForRow,而只是從數組中返回該字符串,而不是為其創建標簽城市。

該錯誤的意思是您要傳遞一個基於NSString的類,而您應該傳遞一個基於UIView的類。

我在這里看不到任何暗示實際發生的東西,因此我希望它return [arrayStates objectAtIndex:row] 您需要返回一個UIView,因此,除非arrayStates除了UIView之外arrayStates包含任何內容,否則這是崩潰的基礎。 修復方法和修復位置可能會更加復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM