0

我有一个UIPickerview。我有json回应。我有一个三个文本字段。如果用户选择第一个文本字段显示选择器视图,则用户选择特定项目。第二个和第三个文本字段也一样。

我的要求是,如果用户在第一个选择器视图中选择特定项目,第二个和第三个选择器视图不显示该项目。第二和第三也一样。用户再次选择第一个文本字段并更改另一个安全问题。然后在这种情况下,第一次选择的安全需要添加到阵列中。第二和第三也一样。所以我正在写下面的代码。我的问题是安全问题在选择器视图中重复出现。

- (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;

}

)
4

0 回答 0