2011-10-18 87 views
0

我爲兩個textField使用一個UIPickerView。一個是國家,另一個是省。我現在遇到的問題是當我選擇國家並返回省UIPickerView時,將國家的相同索引位置!自動選擇UIPickerView位置

下面是代碼

PickerView didSelectedRow事件

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

selectedpickerData = [NSString stringWithFormat:@"%@", [pickerData objectAtIndex:row], row]; 

// Editing Country 
if (activePickerField == 1) { 
    txtCountry.text = selectedpickerData; 
} 
// Editing Province 
else if (activePickerField == 2) { 
    txtProvince.text = selectedpickerData; 
} 
} 

Q1:我應如何針對每一場索引位置?

Q2:如何在textField爲空時選擇UIPickerView的第一個索引位置?

回答

1

UIPickerView具有方法

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated 

它可以用它來選擇0作爲選擇的索引。

然後,對於第二個問題,您需要驗證[UITextField text:isEqualToString:@「」],然後調用上面的方法。例如,當某些控制人辭職時,可能會發生這種情況。

+0

對不起,您如何將選定的選取器索引分配給該字段?我更新了代碼。你能否指出這一點。 – HardCode

+0

selectedpickerData = [NSString stringWithFormat:@「%@」,[pickerData objectAtIndex:row]];那麼你可以在你的if添加時選擇textfield 1來保存選定的索引到一些定義好的int變量,並對第二個做同樣的處理 –

+0

謝謝,工作正常:) – HardCode