我试图在 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;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 210, self.view.frame.size.width-10,30)];
txtField2.delegate = self;
[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 = 是;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-myPickerView.frame.size.height-50, 320, 50)];
[工具栏 setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[工具栏设置项目:工具栏项目];
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = 工具栏;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = 工具栏;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = 工具栏;
}
-(无效)完成:(id)发件人{
[血组辞职FirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}
杂注标记 - UIPickerViewDataSource
// #3 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
返回 1;
}
否则如果(!isBloodGroupFieldSelected){
返回 1;
}
否则如果(!isGenderGroupFieldSelected){
返回 1;
}
返回0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
返回[数据数组计数];
}
否则如果(!isBloodGroupFieldSelected)
{
返回[性别数组计数];
}
否则如果(!isGenderGroupFieldSelected)
{
返回[年龄数组计数];
}
返回0;
}
杂注标记 - UIPickerViewDelegate
// #5 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
返回数据数组[行];
}
否则如果(!isBloodGroupFieldSelected)
{
返回性别数组[行];
}
否则如果(!isGenderGroupFieldSelected)
{
返回年龄数组[行];
}
返回0;
}
// #6 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[行];
}
否则如果(!isBloodGroupFieldSelected)
{
txtField1.text = 性别数组[行];
}
否则如果(!isGenderGroupFieldSelected)
{
txtField2.text=ageArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
否则 if (textField == txtField1){
isBloodGroupFieldSelected = 否;
isGenderGroupFieldSelected = YES;
}
否则 if (textField == txtField2){
isGenderGroupFieldSelected = 否;
isBloodGroupFieldSelected = 否;
}
[myPickerView 重新加载所有组件];
}