如何使用UITextField在同一View控制器中使用多个UIPickerViews

问题描述 投票:0回答:1

我试图在Objective-C的一个视图中拥有多个选择器视图。到目前为止,我已经创建了3个不同的文本字段,如果我单击它们,我希望其中三个具有不同的选择器视图。因此,让我们说,如果单击文本字段#1,则打开数组#1,文本字段#2打开第二个数组,文本字段#第三个数组。如果我单击显示的第一个和第二个文本字段相关的选择器视图,但是如果我单击第三个,则不显示选择器视图。

-(void)viewDidLoad {

isBloodGroupFieldSelected = YES;

isGenderGroupFieldSelected = YES;

//加载视图后进行任何其他设置。

bloodGroup = [[UITextField alloc] initWithFrame:CGRectMake(10,100,self.view.frame.size.width-10,30)];

[self.view addSubview:bloodGroup];

txtField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,160,self.view.frame.size.width-10,30)];

txtField1.delegate =自我;

[self.view addSubview:txtField1];

txtField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,210,self.view.frame.size.width-10,30)]]]

txtField2.delegate =自我;

[self.view addSubview:txtField2];

dataArray = [[NSMutableArray alloc] initWithObjects:@“ A +”,@“ A-”,@“ B +”,@“ B-”,@“ O +”,@“ O-”,nil];

genderArray = [[NSMutableArray alloc] initWithObjects:@“ Male”,@“ Female”,nil];

ageArray = [[NSMutableArray alloc] initWithObjects:@“ Age1”,@“ Age2”,nil];

myPickerView = [[UIPickerView alloc] init];

[myPickerView setDataSource:self];

[[myPickerView setDelegate:self];

myPickerView.showsSelectionIndicator = YES;

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@“完成”样式:UIBarButtonItemStyleDone目标:自我行动:@selector(done:)];

UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-myPickerView.frame.size.height-50、320、50)];

[[toolBar setBarStyle:UIBarStyleBlackOpaque];

NSArray * toolbarItems = [NSArray arrayWithObjects:doneButton,nil];

[[toolBar setItems:toolbarItems];

bloodGroup.inputView = myPickerView;

bloodGroup.inputAccessoryView =工具栏;

// txtField1

txtField1.inputView = myPickerView;

txtField1.inputAccessoryView =工具栏;

txtField2.inputView = myPickerView;

txtField2.inputAccessoryView =工具栏;

}

-(void)done:(id)sender {

[bloodGroup resignFirstResponder];

[[txtField1 resignFirstResponder];

[[txtField2 resignFirstResponder];

}

实用标记-UIPickerViewDataSource

//#3-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

如果(isBloodGroupFieldSelected){

返回1;

}

否则if(!isBloodGroupFieldSelected){

返回1;

}

否则if(!isGenderGroupFieldSelected){

返回1;

}

返回0;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

如果(isBloodGroupFieldSelected){

返回[dataArray计数];

}

否则if(!isBloodGroupFieldSelected)

{

返回[genderArray计数];

}

否则if(!isGenderGroupFieldSelected)

{

返回[ageArray计数];

}

返回0;

}

实用标记-UIPickerViewDelegate

//#5-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

如果(isBloodGroupFieldSelected){

返回dataArray [行];

}

否则if(!isBloodGroupFieldSelected)

{

返回性别数组[行];

}

否则if(!isGenderGroupFieldSelected)

{

return ageArray [row];

}

返回0;

}

//#6-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

如果(isBloodGroupFieldSelected){

bloodGroup.text = dataArray [row];

}

否则if(!isBloodGroupFieldSelected)

{

txtField1.text = sexArray [行];

}

否则if(!isGenderGroupFieldSelected)

{

txtField2.text = ageArray [行];

}

}

  • (void)textFieldDidBeginEditing:(UITextField *)textField {

如果(textField == bloodGroup){

isBloodGroupFieldSelected = YES;

}

否则,如果(textField == txtField1){

isBloodGroupFieldSelected =否;

isGenderGroupFieldSelected = YES;

}

否则,如果(textField == txtField2){

isGenderGroupFieldSelected = NO;

isBloodGroupFieldSelected =否;

}

[myPickerView reloadAllComponents];

}

ios objective-c iphone xcode uipickerview
1个回答
0
投票
为此,您需要同时检查两个条件

(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (isBloodGroupFieldSelected) { return dataArray[row]; } else if(!isBloodGroupFieldSelected) && isGenderGroupFieldSelected { return genderArray[row]; } else if(!isGenderGroupFieldSelected) { return ageArray[row]; } return 0; }

我建议您为此使用textField标记。对您来说会更容易。

© www.soinside.com 2019 - 2024. All rights reserved.