单选按钮问题

时间:2014-11-03 18:16:51

标签: ios

我在我的一个项目中使用了onegray的单选按钮类。这里提到的那个:Best radio-button implementation for IOS

我在测验中使用这些单选按钮作为我的答案选择。当用户单击下一个按钮时,标签将填充新选项。唯一的问题是旧的不会消失。因此,当我单击下一步时,新的按钮组将放置在旧按钮的顶部。

首先检查它们是否已经存在的最简单方法是什么?如果是这样的话......在显示新的之前删除它们?

这是我的代码。

@interface LABViewControllerQuiz ()

@end

@implementation LABViewControllerQuiz
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
int  counter =0;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _fileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"quizQuestions" ofType:@"txt"] encoding:NSUTF8StringEncoding error: nil];
    _theScanner = [NSScanner scannerWithString:_fileContents];
    _separator = [NSCharacterSet characterSetWithCharactersInString:@"~"];
    _lineBreak =[NSCharacterSet characterSetWithCharactersInString:@"@"];
    _alreadyGeneratedNumbers =[[NSMutableArray alloc]init];
    _numQuestions =0;
    _userAnswers = [[NSMutableArray alloc]init];
    _answerKey = [[NSMutableArray alloc]init];

    [self nextQuestion:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (IBAction)nextQuestion:(UIButton *)sender
{
    _NextQuestionButton.enabled = YES;
    _submitButton.enabled = NO;
    NSLog(@"NumQuestion = %d", _numQuestions);
    if (_numQuestions >9)
    {
        _NextQuestionButton.enabled = NO;
        _submitButton.enabled = YES;
    }else
    {
        int r = arc4random() %20;
        while ([_alreadyGeneratedNumbers containsObject:[NSNumber numberWithInt:r]])
        {
            r = arc4random() %20;
        }
        [_alreadyGeneratedNumbers addObject:[NSNumber numberWithInt:r]];


        while(![_theScanner isAtEnd])
        {
            NSLog(@"Location= %d", [_theScanner scanLocation]);
            NSLog(@"Already Generated numbers:");
            int i =0;
            while (i < [_alreadyGeneratedNumbers count])
            {
                NSLog(@"%@", [_alreadyGeneratedNumbers objectAtIndex:i]);
                i++;
            }

            NSString *line;
            _lineArray = [[NSMutableArray alloc] init];
            [_theScanner scanUpToCharactersFromSet:_lineBreak intoString:&line];
            [_theScanner setCharactersToBeSkipped:_lineBreak];
            NSScanner *inner = [NSScanner scannerWithString:line];
            NSString *word;
            int wordCount = 0;
            NSLog(@"r = %d counter = %d", r, counter);
            if (counter ==r)
            {
                while(![inner isAtEnd])
                {
                    [inner scanUpToCharactersFromSet:_separator intoString:&word];
                    [inner setCharactersToBeSkipped:_separator];
                    [_lineArray insertObject:word atIndex:wordCount];
                    _questionText.text = [NSString stringWithFormat:@"Question %d \n %@", _numQuestions +1,[_lineArray objectAtIndex:0]];
                    wordCount++;
                    [_theScanner setScanLocation:0];
                    counter = 0;

                }

                [sender setHidden:YES];
                NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:4];
                CGRect btnRect = CGRectMake(25, 420, 300, 30);
                for (NSString* optionTitle in @[[_lineArray objectAtIndex:1], [_lineArray objectAtIndex:2], [_lineArray objectAtIndex:3], [_lineArray objectAtIndex:4]])
                {
                    RadioButton* btn = [[RadioButton alloc] initWithFrame:btnRect];
                    [btn addTarget:self action:@selector(onRadioButtonValueChanged:) forControlEvents:UIControlEventValueChanged];
                    btnRect.origin.y += 40;
                    [btn setTitle:optionTitle forState:UIControlStateNormal];
                    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
                    [btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
                    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
                    [self.view addSubview:btn];
                    [buttons addObject:btn];
                }


                [buttons[0] setGroupButtons:buttons]; // Setting buttons into the group

                [buttons[0] setSelected:NO]; // Making the first button initially selected

                NSLog(@"the question is = %@", [_lineArray objectAtIndex:0]);
                //NSLog(@"Line arrayINDEX %d = %@", wordCount,[_lineArray objectAtIndex:wordCount]);                _numQuestions ++;
                break;
            }else
            {
                counter ++;
            }

        }
    }
    [_answerKey addObject:[_lineArray objectAtIndex:5]];

}

-(void) onRadioButtonValueChanged:(RadioButton*)sender
{
    // Lets handle ValueChanged event only for selected button, and ignore for deselected
    if(sender.selected)
    {
        NSLog(@"Selected: %@", sender.titleLabel.text);

    }
}

1 个答案:

答案 0 :(得分:0)

buttons保存为实例变量。您已经将所有按钮添加到数组中,您只是出于某种原因抛出数组。

@interface LABViewControllerQuiz ()
@property (strong) NSMutableArray *buttons;
@end

然后这一行:

NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:4];

成为这些界限:

if (self.buttons) {
  [self.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
  [self.buttons removeAllObjects];
} else {
  self.buttons = [NSMutableArray arrayWithCapacity:4];
}