Iphone 使用selectedSegmentIndex的Switch语句
大家怎么办, 这是我第一次在网站上发帖,所以我想我并没有遵循所有的原则 格式和协议正确 我的应用程序有一个包含问题列表的UIView,每个问题都有一个名为“question_btn”的UISegmentedControl。UISegmentedControl有两个部分,第一个部分表示“否”,第二个部分表示“是”,对应于每个问题 每个“question_btn”单独调用-(void)processQuestions{},但是当我单击每个“question_btn”时,只有最后一个被正确处理。所有以前的“问题”都调用processQuestions方法,但不实现其代码 我对UISegmentControl或switch语句或两者都有问题 如有任何建议,将不胜感激 按钮的定义如下Iphone 使用selectedSegmentIndex的Switch语句,iphone,uisegmentedcontrol,Iphone,Uisegmentedcontrol,大家怎么办, 这是我第一次在网站上发帖,所以我想我并没有遵循所有的原则 格式和协议正确 我的应用程序有一个包含问题列表的UIView,每个问题都有一个名为“question_btn”的UISegmentedControl。UISegmentedControl有两个部分,第一个部分表示“否”,第二个部分表示“是”,对应于每个问题 每个“question_btn”单独调用-(void)processQuestions{},但是当我单击每个“question_btn”时,只有最后一个被正确处理。所有以
question_btn = [[[UISegmentedControl alloc] initWithFrame:CGRectMake(QUESTION_BUTTON_XPOS, questionsButton_YPos, 80, 20)] autorelease];
[question_btn insertSegmentWithTitle:@"No" atIndex:0 animated:NO];
[question_btn insertSegmentWithTitle:@"Yes" atIndex:1 animated:NO];
[question_btn setEnabled:YES forSegmentAtIndex:0];
[question_btn setEnabled:YES forSegmentAtIndex:1];
question_btn.segmentedControlStyle = UISegmentedControlStyleBar;
question_btn.tintColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
[question_btn addTarget:self action:@selector(processQuestions) forControlEvents:UIControlEventValueChanged];
下面是一些问题
- (void) processQuestions
{
//NSLog(@"QuestionsView processQuestions: Called");
//Process the questions i.e. determine whether yes or no was the answer
answersArray = [[NSMutableArray alloc] init];
int selectedIndex;
selectedIndex = [question_btn selectedSegmentIndex];
switch (selectedIndex)
{
case 0:
NSLog(@"QuestionsView processQuestions: No answered");
break;
case 1:
NSLog(@"QuestionsView processQuestions: Yes answered");
break;
}
[answersArray addObject:[NSNumber numberWithInt:selectedIndex]];
NSLog(@"QuestionsView processQuestions: answersArray contains: %@", answersArray);
}
下面是我如何让代码正确地迭代并获得问题_btnselected索引值的。只需要创建一个可变数组来保存问题对象,
然后使用for循环为每个按钮设置选定的索引值。
buttonsArray是在我的createView方法中设置的
buttonsArray = [[NSMutableArray alloc] init];
- (void) processQuestions
{
//NSLog(@“问题视图流程问题:调用”)
answersArray=[[NSMutableArray alloc]init];
int-selectedIndex;
for(int i=0;i
}如果您有一系列问题,可以使用分段控件数组。您可能有一个按钮“Get Result”,因此按下时,您将遍历所有分段控件,并在其中选中所选索引(是/否-0/1)
现在,在上面的代码中,为每个分段控件的状态更改调用processQuestions。如果默认值由用户保留,则不会将结果添加到应答数组中。能否正确地重新插入代码?所有分段控件都添加到同一视图中?您如何知道调用processQuestions方法的是哪个问题?您好karim,我不确定您说“如果默认值由用户保留”是什么意思。[问题\u btn addTarget:自我操作:@selector(processQuestions)forControlEvents:UIControlEventValueChanged];因此,仅当用户点击segment控件时才会调用processQuestions。如果用户不点击,并认为默认选择的索引表示正确答案,该怎么办?
answersArray = [[NSMutableArray alloc] init];
int selectedIndex;
for (int i = 0; i < numQuestions; i ++)
{
question_btn = (UISegmentedControl*)[buttonsArray objectAtIndex:i];
selectedIndex = [question_btn selectedSegmentIndex];
switch (selectedIndex)
{
case 0:
NSLog(@"QuestionsView processQuestions: No answered for question: %d", i + 1);
break;
case 1:
NSLog(@"QuestionsView processQuestions: Yes answered for question: %d", i + 1);
break;
}
[answersArray addObject:[NSNumber numberWithInt:selectedIndex]];
}
NSLog(@"QuestionsView processQuestions: answersArray contains: %@", answersArray);