簡體   English   中英

如何閱讀UIPicker選定的文本

[英]How to read UIPicker selected text

我創建了一個UIPicker,其中包含大約八或九個可能的字符串。 根據加載的視圖,UIPicker將取決於顯示的字符串。

由於這在我的pickerView:didSelectRow:inComponent中,所以我想知道是否有一種方法可以讓用戶選擇行,如果我可以獲取其中的textvalue。

這樣,在didselectrow方法中,我可以根據所選的字符串值執行不同的操作。

當前,這就是我所擁有的:

#pragma mark - Picker Delegates
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

// get rowString somehow?
    // ReloadView
    if ([rowString isEqualToString:@"one Of the Values"]) {

     }
    // more if statements here.
}

只需從數據源中讀取該行的值,然后進行比較即可:

NSString *rowString = (NSString *)[self.pickerArray objectAtIndex:row];
if ([rowString isEqualToString:@"one Of the Values"]) {

 }

@Meda給了您正確的答案。 您不應將數據存儲在選擇器之類的視圖對象中。 請記住,為了使UIPickerView正常工作,必須實現方法pickerView:titleForRow:forComponent:' (or the method :forComponent:'(如果您自己構建單元格視圖,則必須實現方法pickerView:titleForRow:forComponent:' (or the method viewForRow:forComponent:reusingView:')。 `pickerView:titleForRow:forComponent:'方法為您的選擇器視圖的組件中的每一行提供文本,因此它必須具有可用的數據。

因此,UIPickerView的委托需要使該數據可用。 Meda的帖子假定您將其保存在數組pickerArray

[pickerView selectedRowInComponent:0];

暫無
暫無

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

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