我可以将一个UIPickerView与多个UITextField一起使用,还是应该创建多个pickerviews,每个UITextField一个(我在同一个视图中拥有所有UITextField)?
答案 0 :(得分:4)
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField1.editing == YES)
{
textFieldName=textField1;
}
else
if (textField2.editing == YES)
{
textFieldName=textField2;
}
}
答案 1 :(得分:3)
使用数组列表视图,然后获取位置。
答案 2 :(得分:2)
请全局声明此textFieldName:
NSString * textFieldName;
didLoad中的分配:
- (void)viewDidLoad
{
[super viewDidLoad];
[pickerView setHidden:YES];
textFieldName=[NSString alloc]init];
pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"apple", @"mango", @"banana", nil];
pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"black", @"white", @"green", nil];
}
请在textFieldName字符串中设置文本字段名称:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (textField1.editing == YES)
{
textFieldName=textField1;
[pickerView setHidden:NO];
}
else
if (textField2.editing == YES)
{
textFieldName=textField2;
[pickerView setHidden:NO];
}
}
然后使用此textFieldName检查pickerview方法:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (textFieldName isEqualToString:@"textField1")
{
return [pickerArray1 count];
}
else
if (textFieldName isEqualToString:@"textField2")
{
return [pickerArray2 count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (textFieldName isEqualToString:@"textField1")
{
return [pickerArray1 objectAtIndex:row];
}
else
if (textFieldName isEqualToString:@"textField2")
{
return [pickerArray2 objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (textFieldName isEqualToString:@"textField1")
{
textField1.text= [pickerArray1 addObjectAtIndex:row];
}
else
if (textFieldName isEqualToString:@"textField2")
{
textField2.text= [pickerArray2 addObjectAtIndex:row];
}
[pickerView setHidden:YES];
}
答案 3 :(得分:0)
我认为一个具有多个文本字段的Picker视图就足够了。您可以轻松标记文本字段并根据需要进行处理。
Multiple sources for UIPickerView on textfield editing
将帮助您入门