I am trying to have multiple picker views in one single view in Objective-C. So far I have created 3 different textfields and I want three of them to have different picker views if I click on them. so let us say if click on textfield #1 it opens the array #1 and textfields #2 the second and textfields # third one. If i click the first and second text fields related picker view displaying but if i click the third one picker view is not showing.
-(void)viewDidLoad{
isBloodGroupFieldSelected = YES;
isGenderGroupFieldSelected = YES;
// Do any additional setup after loading the view.
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 = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDonetarget:self action:@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 = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(void)done:(id)sender{
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}
pragma mark - UIPickerViewDataSource
// #3-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected){
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [genderArray count];
}
else if(!isGenderGroupFieldSelected)
{
return [ageArray count];
}
return 0;
}
pragma mark - UIPickerViewDelegate
// #5-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return dataArray[row];
}
else if(!isBloodGroupFieldSelected)
{
return genderArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return ageArray[row];
}
return 0;
}
// #6-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if(!isBloodGroupFieldSelected)
{
txtField1.text = genderArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= ageArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2){
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}