我创建了一个quizz应用程序。
有用于显示问题的标签和用于显示答案的四个按钮。
单击下一个按钮时,所有选项都会更改并从阵列中获取。
但是点击上一个按钮时,所有复选框都被清除了。如何存储以前选择的按钮。
这是我的代码
- (void)viewDidLoad
{
[super viewDidLoad];
k = 0;
ansCount = 0;
NSLog(@"%d",ansCount);
self.btnFinish.hidden = YES;
self.btnPrevious.hidden = YES;
// Do any additional setup after loading the view.
arrAddQues = [[NSMutableArray alloc]init];
arrAddOption1 = [[NSMutableArray alloc]init];
arrAddOption2 = [[NSMutableArray alloc]init];
arrAddOption3 = [[NSMutableArray alloc]init];
arrAddOption4 = [[NSMutableArray alloc]init];
arrAns = [[NSMutableArray alloc]init];
Facilities *facility = [[Facilities alloc]init];
NSMutableArray *arrayData = [facility selectData:[NSString stringWithFormat:@"select * from ios"]];
for (int i = 0; i < arrayData.count ; i++) {
NSLog(@"%@",[arrayData objectAtIndex:i]);
}
for(NSDictionary *dict in arrayData)
{
NSString *str = [dict valueForKey:@"question"];
NSString *str1 = [dict valueForKey:@"option1"];
NSString *str2 = [dict valueForKey:@"option2"];
NSString *str3 = [dict valueForKey:@"option3"];
NSString *str4 = [dict valueForKey:@"option4"];
NSString *str5 = [dict valueForKey:@"ans"];
NSLog(@"%@",str);
[arrAddQues addObject:[NSString stringWithFormat:@"%@",str]];
[arrAddOption1 addObject:[NSString stringWithFormat:@"%@",str1]];
[arrAddOption2 addObject:[NSString stringWithFormat:@"%@",str2]];
[arrAddOption3 addObject:[NSString stringWithFormat:@"%@",str3]];
[arrAddOption4 addObject:[NSString stringWithFormat:@"%@",str4]];
[arrAns addObject:[NSString stringWithFormat:@"%@",str5]];
}
NSLog(@"Final Array..............%@",arrAddQues);
NSLog(@"Ans:---%@",arrAns);
self.lblQues1.text = [arrAddQues objectAtIndex:0];
[self.btnOption1 setTitle:[arrAddOption1 objectAtIndex:0] forState:UIControlStateNormal];
[self.btnOption2 setTitle:[arrAddOption2 objectAtIndex:0] forState:UIControlStateNormal];
[self.btnOption3 setTitle:[arrAddOption3 objectAtIndex:0] forState:UIControlStateNormal];
[self.btnOption4 setTitle:[arrAddOption4 objectAtIndex:0] forState:UIControlStateNormal];
}
- (IBAction)click_Previous:(id)sender {
if (k == 1) {
self.btnPrevious.hidden = YES;
k--;
self.lblQues1.text = [arrAddQues objectAtIndex:k];
[self.btnOption1 setTitle:[arrAddOption1 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption2 setTitle:[arrAddOption2 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption3 setTitle:[arrAddOption3 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption4 setTitle:[arrAddOption4 objectAtIndex:k] forState:UIControlStateNormal];
}
else if (k < arrAddQues.count) {
NSLog(@"value %d",k);
self.btnNext.hidden = NO;
self.btnPrevious.hidden = NO;
self.btnFinish.hidden = YES;
k--;
self.lblQues1.text = [arrAddQues objectAtIndex:k];
[self.btnOption1 setTitle:[arrAddOption1 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption2 setTitle:[arrAddOption2 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption3 setTitle:[arrAddOption3 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption4 setTitle:[arrAddOption4 objectAtIndex:k] forState:UIControlStateNormal];
ansCount--;
NSLog(@"%d",ansCount);
if (k == arrAddQues.count -1) {
k--;
self.btnNext.hidden = YES;
self.btnFinish.hidden = NO;
self.btnPrevious.hidden = NO;
}
}
}
- (IBAction)click_Next:(id)sender {
if (k < arrAddQues.count -1) {
self.btnNext.hidden = NO;
self.btnFinish.hidden = YES;
self.btnPrevious.hidden = NO;
if (temp == 1) {
temp = 0;
if ([self.btnOption1.currentTitle isEqualToString:[arrAns objectAtIndex:k]]) {
ansCount++;
NSLog(@"%d",ansCount);
}
}
else if(temp == 2)
{
temp = 0;
if ([self.btnOption2.currentTitle isEqualToString:[arrAns objectAtIndex:k]]) {
ansCount++;
NSLog(@"%d",ansCount);
}
}
else if (temp == 3)
{
temp = 0;
if ([self.btnOption3.currentTitle isEqualToString:[arrAns objectAtIndex:k]]) {
ansCount++;
NSLog(@"%d",ansCount);
}
}
else if (temp == 4)
{
temp = 0;
if ([self.btnOption4.currentTitle isEqualToString:[arrAns objectAtIndex:k]]) {
ansCount++;
NSLog(@"%d",ansCount);
}
}
else
{
NSLog(@"Wrong Answer!!!!!!!");
}
k++;
self.lblQues1.text = [arrAddQues objectAtIndex:k];
[self.btnOption1 setTitle:[arrAddOption1 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption2 setTitle:[arrAddOption2 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption3 setTitle:[arrAddOption3 objectAtIndex:k] forState:UIControlStateNormal];
[self.btnOption4 setTitle:[arrAddOption4 objectAtIndex:k] forState:UIControlStateNormal];
self.imageOp1.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
if (k == arrAddQues.count -1) {
self.btnNext.hidden = YES;
self.btnFinish.hidden = NO;
}
}
}
- (IBAction)click_Option1:(UIButton *)sender {
temp = sender.tag;
self.imageOp1.image = [UIImage imageNamed:@"checkcox-tic.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
}
- (IBAction)click_Option2:(UIButton *)sender {
temp = sender.tag;
self.imageOp1.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox-tic.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
}
- (IBAction)click_Option3:(UIButton *)sender {
temp = sender.tag;
self.imageOp1.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox-tic.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
}
- (IBAction)click_Option4:(UIButton *)sender {
temp = sender.tag;
self.imageOp1.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox-tic.png"];
}
答案 0 :(得分:0)
You need to save selected item on - (IBAction)click_Option:(UIButton *)sender to ur array
add one more field to ur array like **selectedAns:"1"**
- (IBAction)click_Previous:(id)sender {
sender=[arr objectatindex:k];
switch(selectedAns)
{
case 1:
self.imageOp1.image = [UIImage imageNamed:@"checkcox-tic.png"];
self.imageOp2.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
case 2:
self.imageOp2.image = [UIImage imageNamed:@"checkcox-tic.png"];
self.imageOp1.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp3.image = [UIImage imageNamed:@"checkcox.png"];
self.imageOp4.image = [UIImage imageNamed:@"checkcox.png"];
case 3:
case 4:
}
}
Save data to this format Array{ dictionary {
question:@"",ans1:"",ans2:@""...}, dictionary{question:@"",ans1:"",ans2:@""...},.....}
for easy access, Hope this will help u