`iphone sdk中的多个单选按钮验证

时间:2012-10-10 04:50:42

标签: iphone objective-c ios

我想要进行验证in Multiple dynamic单选按钮. I want to check单选按钮is是否已选中。如果没有选择,则显示警告并选择然后转到另一件事。

以下代码我使用..

-(IBAction)btnNextClicked:(id)sender{
    if ([appDelegate.questions count]> i) {


        for (UIButton *btn in self.view.subviews)
        {
            if ([btn isKindOfClass:[UIButton class]])
            {
                if (btn.selected)
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"BizVibe" message:@"Please select answer" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                    return;
                }
                else{
                    for (int k=0; k<[[[appDelegate.questions objectAtIndex:i-1] objectForKey:[[appDelegate.questions objectAtIndex:i-1] objectForKey:@"question"]] count]; k++) {

                        for (UILabel *lbl in self.view.subviews)
                        {
                            if ([lbl isKindOfClass:[UILabel class]])
                            {
                                if (lbl.tag==k)
                                {
                                    [lbl removeFromSuperview];
                                }
                            }

                        }
                        for (UIButton *btn in self.view.subviews)
                        {
                            if ([btn isKindOfClass:[UIButton class]])
                            {
                                if (btn.tag==k)
                                {
                                    [btn removeFromSuperview];
                                }
                            }

                        }
                    }
                    [self LoadQuestionAnswer];
                    return;

                }
            }

        }


    }
    else{
        Term_para *termMSP = [[Term_para alloc]init];
        [self.navigationController pushViewController:termMSP animated:YES];
        [termMSP release];
    }
}

请帮助我,我能做些什么?

提前致谢..

2 个答案:

答案 0 :(得分:1)

我在代码中无法提供帮助,因为我不知道您希望在该代码体中做什么,但您可以使用selected变量处理按钮UnSelectedBOOL它取决于你的逻辑。见下面的例子 -

BOOL isSelected; // Declared it globally

-(IBAction)btnClicked:(id)sender
{
    if(isSelected)
    {
        [btn setTitle:@"Unselected" forState:UIControlStateNormal];
    }
    else
    {
        [btn setTitle:@"Selected" forState:UIControlStateNormal];
    }

    isSelected = !isSelected;
}

答案 1 :(得分:0)

以下是iOS中的单选按钮功能tutorial。看看这个。它可能会引导您走向正确的方向。