I have one UIPickerview
. I have a json
response. I have a three text fields. If user select the first text field display picker view then user select the particular item. And second and third text field also same.
My requirement is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same. Again user selects the first text field and change another security question. Then in this scenario first time selected security needs to be added to the array. Second and third also same. So I am writing the below code. My problem is security questions are repeating in picker view.
- (void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
QArray = [[NSMutableArray alloc]init];
QidArray = [[NSMutableArray alloc]init];
pickerviewRowCount++;
NSMutableDictionary *secQuestions=questionInfo[@"SecurityQuestion"];
NSLog(@"Dictionary values of security question is :%@",secQuestions);
for (NSDictionary *dict in secQuestions){
pickerviewRowCount++;
[QidArray addObject:[dict valueForKey:@"QuestionID"]];
[QArray addObject:[dict valueForKey:@"Question"]];
NSLog(@"QID: %@ , Q:%@",[dict valueForKey:@"QuestionID"],[dict valueForKey:@"Question"]);
}
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, passManagerLbl.frame.origin.y+passManagerLbl.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.placeholder = @"Please Select Your Option";
[scrollview addSubview:bloodGroup];
ansField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, bloodGroup.frame.origin.y+bloodGroup.frame.size.height+45, screenWidth-20, 50)];
ansField1.placeholder = @"Please enter Answer";
ansField1.delegate = self;
[scrollview addSubview:ansField1];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.placeholder = @"Please Select Your Option";
[scrollview addSubview:txtField1];
ansField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, txtField1.frame.origin.y+txtField1.frame.size.height+45, screenWidth-20, 50)];
ansField2.placeholder = @"Please enter Answer";
[scrollview addSubview:ansField2];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.placeholder = @"Please Select Your Option";
[scrollview addSubview:txtField2];
ansField3 = [[UITextField alloc]initWithFrame:CGRectMake(10, txtField2.frame.origin.y+txtField2.frame.size.height+45, screenWidth-20, 50)];
ansField3.placeholder = @"Please enter Answer";
[scrollview addSubview:ansField3];
}
-(void)done:(id)sender
{
[self pickerData];
}
-(void)pickerData
{
int row = [myPickerView selectedRowInComponent:0];
if (isBloodGroupFieldSelected) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:0 withObject:dict];
selectedValue1 = QidArray[row];
bloodGroup.text = QArray[row];
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
selectedItem1=bloodGroup.text;
NSLog(@"Selected row value 1 of Picker view is:%@",selectedItem1);
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:1 withObject:dict];
txtField1.text = QArray[row];
selectedItem2 = txtField1.text;
selectedValue2 = QidArray[row];
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
}
else if(!isGenderGroupFieldSelected)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:2 withObject:dict];
txtField2.text= QArray[row];
selectedItem3= txtField2.text;
selectedValue3 = QidArray[row];
NSLog(@"Selected row value 3 of Picker view is.....:%@",selectedValue3);
NSLog(@"selected item value 1 of picker view is:%@",selectedItem3);
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
}
[myPickerView selectRow:0 inComponent:0 animated:YES];
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
// #4
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return [QArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [QArray count];
}
else if(!isGenderGroupFieldSelected)
{
return [QArray count];
}
return 0;
}
#pragma mark - UIPickerViewDelegate
// #5
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return QArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return QArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return QArray[row];
}
return 0;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:0]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSString *selectInd = [dict objectForKey:@"selectedIndex"]?[dict objectForKey:@"selectedIndex"]:@"";
// [dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
[QidArray insertObject:selectInd atIndex:0];
}
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:1]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSString *selectInd = [dict objectForKey:@"selectedIndex"]?[dict objectForKey:@"selectedIndex"]:@"";
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
[QidArray insertObject:selectInd atIndex:0];
}
}
else if (textField == txtField2){
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:2]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSString *selectInd = [dict objectForKey:@"selectedIndex"]?[dict objectForKey:@"selectedIndex"]:@"";
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
[QidArray insertObject:selectInd atIndex:0];
}
}
[myPickerView reloadAllComponents];
}
This is my json Response:
security question is :(
{
Question = "What is your first pet's name?";
QuestionID = 23;
},
{
Question = "Who was your first grade teacher?";
QuestionID = 24;
},
{
Question = "What city were you born in?";
QuestionID = 25;
},
{
Question = "What was your childhood nickname?";
QuestionID = 26;
},
{
Question = "What was the name of your elementary school?";
QuestionID = 27;
},
{
Question = "Where does your nearest relative live?";
QuestionID = 28;
},
{
Question = "In what city or town was your first job?";
QuestionID = 29;
},
{
Question = "Which is your first car? Edited ";
QuestionID = 30;
},
{
Question = "This is a test question";
QuestionID = 32;
},
{
Question = "What is your Mother's maiden name?";
QuestionID = 33;
},
{
Question = "What is your Last name?";
QuestionID = 35;
},
{
Question = "What is the last 4 of your SSN?";
QuestionID = 36;
},
{
Question = "What is your Date of Birth? (MM/DD/YYYY)";
QuestionID = 37;
}
)