您好我正在开发一个iphone应用程序,我已经填充了一个uipickerview,它可以很好地使用一个uitextfield。但是我需要填充4个uitextfield,并且我为每个UItextfield使用相同的uipickerview。我不知道如何实现这个。
Pickerviewcontroller.m
arrChooseServices=[[NSMutableArray alloc]initWithObjects:@"A+ve",@"A-ve",@"B+ve",@"B-ve",@"AB+ve",@"AB-ve",@"O+ve",@"O-ve", nil];
// Create UITextfield
bloodGroup=[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 120, 20)];
bloodGroup.borderStyle=UITextBorderStyleRoundedRect;
bloodGroup.backgroundColor=[UIColor clearColor];
bloodGroup.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
[bloodGroup setUserInteractionEnabled:NO];
bloodGroup.font=[UIFont systemFontOfSize:14.0];
bloodGroup.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
bloodGroup.textAlignment=NSTextAlignmentCenter;
bloodGroup.delegate=self;
bloodGroup.inputView=self.chooseServicePicker;
bloodGroup.inputAccessoryView=self.accessoryView;
[self.view addSubview:bloodGroup];
-(UIPickerView *)chooseServicePicker
{
if(chooseServicePicker==nil)
{
chooseServicePicker=[[UIPickerView alloc]init];
chooseServicePicker.delegate=self;
chooseServicePicker.dataSource=self;
chooseServicePicker.showsSelectionIndicator=YES;
}
return chooseServicePicker;
}
-(UIToolbar *)accessoryView
{
if ( accessoryView == nil )
{
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onVehicleSelection)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
accessoryView.items=[NSArray arrayWithObjects:doneButton,cancelButton, nil];
}
return accessoryView;
}
- (void)onVehicleSelection
{
NSInteger row = [self.chooseServicePicker selectedRowInComponent:0];
bloodGroup.text=[arrChooseServices objectAtIndex:row];
[bloodGroup resignFirstResponder];
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrChooseServices objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return ( arrChooseServices == nil ) ? 0 : [arrChooseServices count];
}
-(void)cancel
{
bloodGroup.text=@"";
[bloodGroup resignFirstResponder];
}
这是我的代码。现在我想使用相同的UIPickerview
在另一个文本字段中显示另一个数组值gender=[NSMutableArray alloc]initWithObjectis:@"MALE",@"Female",nil];
我想在Uipickerview中添加上面的数组值。如何编写代码。请给我任何想法。我是编程新手。提前致谢
答案 0 :(得分:1)
您可以使用它来检查转到becomeFirstResponder
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
并基于此条件
[textField isFirstResponder]
您可以检查您需要显示的内容。
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(textField1 isFirstResponder]
return [arrChooseServices objectAtIndex:row];
else if(textField2 isFirstResponder] return [otherArray count];
// write for every textfield other
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if(textField1 isFirstResponder] return 1;
else if(textField2 isFirstResponder] return 3;
//In this way you can do what you need to show in picker view.
return 1;
}
//Same condition you can use in every delegate and datasource method.
答案 1 :(得分:1)
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDelegate,UITextFieldDelegate>
{
UIPickerView *myPickerView;
int selectedTextfieldValue;
NSArray *namesArray , *genderArray, *ageArray;
}
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
namesArray=[[NSArray alloc]initWithObjects:@"a",@"b", nil];
genderArray=[[NSArray alloc]initWithObjects:@"M",@"F", nil];
ageArray=[[NSArray alloc]initWithObjects:@"26",@"25", nil];
int yaxis =50;
selectedTextfieldValue=0;
for (int i=0; i<5; i++)
{
UITextField* bloodGroup=[[UITextField alloc]initWithFrame:CGRectMake(150, yaxis, 120,20)];
bloodGroup.borderStyle=UITextBorderStyleRoundedRect;
bloodGroup.backgroundColor=[UIColor clearColor];
bloodGroup.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
// bloodGroup.tag=(int)i;// first tag will be ZERO but by default view tag is ZERO so
bloodGroup.tag=(int)[[NSString stringWithFormat:@"20%d",i] intValue];
bloodGroup.font=[UIFont systemFontOfSize:14.0];
bloodGroup.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
bloodGroup.textAlignment=NSTextAlignmentCenter;
bloodGroup.delegate=self;
[self.view addSubview:bloodGroup];
yaxis=yaxis +30;
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
NSLog(@"%d",textField.tag);
selectedTextfieldValue=(int)textField.tag;
// call picker here according to textfield tag you can set text to textfiled
[self popoverWithInformation];
}
-(void)popoverWithInformation
{
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
myPickerView = [[UIPickerView alloc] init];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
CGRect pickerRect = myPickerView.bounds;
myPickerView.bounds = pickerRect;
myPickerView.frame = CGRectMake(0, 44, 320, 216);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 300)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:myPickerView];
[popoverView addSubview:pickerToolbar];
[self.view addSubview:popoverView];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
}
// tell the picker how many rows are available for a given component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
int count = 0;
switch (selectedTextfieldValue) {
case 200:
count =namesArray.count;
break;
case 201:
count =genderArray.count;
break;
case 202:
count =ageArray.count;
break;
default:
break;
}
return count;
}
// tell the picker how many components it will have
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
switch (selectedTextfieldValue) {
case 200:
title =namesArray[row];
break;
case 201:
title =genderArray[row];
break;
case 202:
title =ageArray[row];
break;
default:
break;
}
return title;
}
// tell the picker the width of each row for a given component
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
-(void)pickerDone:(id)sender
{
}
-(void)pickerCancel:(id)sender
{
}
答案 2 :(得分:0)
如果我正确理解你的问题,我想你需要多列UIPickerView。
您需要在此UIPickerViewDatasource方法中定义列数:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
然后在这个方法中:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
您需要返回每个组件的行数。
实施例::
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3; // for four columns
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _array1.count; // 10 rows in 1st column
}
else if(component == 1)
{
return _array2.count; // 15 rows in 1st column
}
else if(component == 2)
{
return _array3.count; // 15 rows in 1st column
}
}
根据UIPickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
self.textfield1.text = [_array1 objectAtIndex:row];
}
else if(component == 1)
{
self.textfield2.text = [_array2 objectAtIndex:row];
}
else if(component == 2)
{
self.textfield3.text = [_array3 objectAtIndex:row];
}
}
答案 3 :(得分:-1)
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.inputView = pickerView;
//Set the Tag of Uipickerview
pickerView.tag = textField.tag; // you could workaround with tag in delegate methods accordingly
[pickerView reloadAllComponents];
return YES;
}