我的应用中有很多UIPickers。我让他们设置当用户点击文本控件时,出现选择器,用户可以选择一个值。然后该值显示在文本字段中,但我希望在UIPicker出现时使用初始值填充文本字段。目前,在用户物理移动选择器之前,不会填充文本字段。
代码:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if([pickerView isEqual: routePicker])
{
route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
}
else if([pickerView isEqual: timePicker])
{
if (component == 0)
{
selectedHour = [hourArray objectAtIndex:row];
}
else if (component == 1)
{
selectedMinute = [minuteArray objectAtIndex:row];
}
else if (component == 2)
{
selectedSecond = [secondArray objectAtIndex:row];
}
time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond];
}
else if([pickerView isEqual: activityPicker])
{
activity.text = [activityArray objectAtIndex:row];
}
else if([pickerView isEqual: intensityPicker])
{
intensity.text = [intensityArray objectAtIndex:row];
}
}
此致 斯蒂芬
答案 0 :(得分:0)
在textfield委托方法上 在打开选择器之前
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.tag==kINTENSITYTEXTTAG)
{
if(![intensity.text length])
{
intensity.text = [intensityArray objectAtIndex:0];
}
}
// SAME FOR REMAINING textFields Based on the tag to get which textfield is selected
}
快乐的编码......