Ios 单选按钮问题
我正在我的一个项目中使用onegray的单选按钮类。这里提到的是: 我在测验中用这些单选按钮选择答案。当用户单击“下一步”按钮时,标签将填充新选项。唯一的问题是旧的不会消失。因此,当我单击“下一步”时,新的按钮集被放置在旧按钮的顶部 首先检查它们是否已经存在的最简单方法是什么。。如果是的话。。删除它们。。在展示新产品之前 这是我的密码Ios 单选按钮问题,ios,Ios,我正在我的一个项目中使用onegray的单选按钮类。这里提到的是: 我在测验中用这些单选按钮选择答案。当用户单击“下一步”按钮时,标签将填充新选项。唯一的问题是旧的不会消失。因此,当我单击“下一步”时,新的按钮集被放置在旧按钮的顶部 首先检查它们是否已经存在的最简单方法是什么。。如果是的话。。删除它们。。在展示新产品之前 这是我的密码 @interface LABViewControllerQuiz () @end @implementation LABViewControllerQuiz
@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);
}
}
@interface labviewcontrollerquick()
@结束
@LabVIEWControllerQuike的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
int计数器=0;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
_fileContents=[NSString stringWithContentsOfFile:[NSBundle mainBundle]pathForResource:@“txt”类型的“quizQuestions”编码:NSUTF8StringEncoding错误:nil];
_扫描仪=[NSScanner SCANNER WITHSTRING:_fileContents];
_分隔符=[NSCharacterSet characterSetWithCharactersInString:@“~”];
_lineBreak=[NSCharacterSet characterSetWithCharactersInString:@“@”];
_alreadyGeneratedNumbers=[[NSMutableArray alloc]init];
_numQuestions=0;
_userAnswers=[[NSMutableArray alloc]init];
_answerKey=[[NSMutableArray alloc]init];
[自我下一个问题:无];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
-(iAction)下一个问题:(UIButton*)发送者
{
_NextQuestionButton.enabled=是;
_submitButton.enabled=否;
NSLog(@“NumQuestion=%d”,numQuestions);
如果(_numQuestions>9)
{
_NextQuestionButton.enabled=否;
_submitButton.enabled=是;
}否则
{
int r=arc4random()%20;
而([\u alreadyGeneratedNumbers containsObject:[NSNumber numberWithInt:r]])
{
r=arc4random()%20;
}
[\u alreadyGeneratedNumbers添加对象:[NSNumber numberWithInt:r]];
而(![[扫描者正在扫描])
{
NSLog(@“位置=%d”,[\u扫描仪位置];
NSLog(@“已生成编号:”);
int i=0;
而(i<[[u已生成的数字计数])
{
NSLog(@“%@,[\u alreadyGeneratedNumbers对象索引:i]);
i++;
}
NSString*行;
_lineArray=[[NSMutableArray alloc]init];
[\u扫描仪扫描字符从集合:\u换行到字符串:&line];
[\u扫描仪设置字符已禁用:\u lineBreak];
NSScanner*内部=[NSScanner scannerWithString:line];
NSString*字;
int字数=0;
NSLog(@“r=%d计数器=%d”,r,计数器);
如果(计数器==r)
{
而(![inner Isattend])
{
[内部扫描字符从集合:_分隔符到字符串:&word];
[内部设置字符stobeskiped:_分隔符];
[_linearrayinsertobject:word-atIndex:wordCount];
_questionText.text=[NSString stringWithFormat:@“问题%d\n%@”,\u numQuestions+1,[\u lineArray objectAtIndex:0]];
字数++;
[_扫描仪设置扫描位置:0];
计数器=0;
}
[发件人设置隐藏:是];
NSMutableArray*按钮=[NSMutableArray阵列容量:4];
CGRect btnRect=CGRectMake(25420300,30);
对于(NSString*OPTIONTILE在@[[U lineArray objectAtIndex:1]、[U lineArray objectAtIndex:2]、[U lineArray objectAtIndex:3]、[U lineArray objectAtIndex:4]]
{
RadioButton*btn=[[RadioButton alloc]initWithFrame:btnRect];
[btn addTarget:self action:@selector(onRadioButtonValueChanged:)for ControlEvents:UIControlEventValueChanged];
b精确原点y+=40;
[btn setTitle:optionTitle for状态:UIControlStateNormal];
[btn setTitleColor:[UIColor darkGrayColor]用于状态:UIControlStateNormal];
btn.titleLabel.font=[UIFont boldSystemFontOfSize:17];
[btn setImage:[UIImage ImageName:@“unchecked.png”]用于状态:UIControlStateNormal];
[btn setImage:[UIImage ImageName:@“checked.png”]用于状态:UIControlStateSelected];
btn.contentHorizontalAlignment=uicontrol内容HorizontalAlignment左侧;
btn.titleEdgeInsets=UIEdgeInsetsMake(0,6,0,0);
[self.view addSubview:btn];
[按钮添加对象:btn];
}
[按钮[0]设置组按钮:按钮];//将按钮设置到组中
[按钮[0]setSelected:否];//使第一个按钮最初处于选中状态
NSLog(@“问题是=%@,[\u lineArray objectAtIndex:0]);
//NSLog(@“Line arrayINDEX%d=%@”,字数,[[u lineArray objectAtIndex:wordCount]);\u numQuestions++;
打破
}否则
{
计数器++;
}
}
}
[\u answerKey addObject:[\u lineArray objectAtIndex:5]];
}
-(无效)onRadioButtonValueChanged:(RadioButton*)发送方
{
//仅允许为sele处理ValueChanged事件
@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];
}