使用 selectedSegmentIndex 的 Switch 语句

发布于 2024-09-30 11:00:28 字数 2990 浏览 0 评论 0原文

大家好,

这是第一次在该网站上发帖,所以我想我还没有关注所有内容 格式和协议正确。

我的应用程序有一个包含问题列表的 UIView,以及每个问题的名为“question_btn”的 UISegmentedControl。 UISegmentedControl 有 2 个部分,第一个部分代表“否”答案,第二个部分代表“是”答案,对应于每个问题。

每个“question_btn”单独调用 -(void) processQuestions{},但是当我单击每个“question_btn”时,只有最后一个被正确处理。之前的所有“question_btns”都调用processQuestions方法,但没有实现其代码。

我的问题是 UISegmentControl 或 switch 语句,或两者兼而有之。

任何建议将不胜感激。

该按钮定义如下:

 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]; 

下面是 processQuestions

- (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);
}

这是我如何让代码正确迭代并获取 Question_btn 选定的索引值。只需创建一个可变数组来保存 Question_btn 对象即可, 然后使用 for 循环为每个按钮设置选定的索引值。 ButtonArray 在我的 createView 方法中设置。

buttonsArray = [[NSMutableArray alloc] init];

- (void) processQuestions

{ //NSLog(@"Questions查看过程Questions:已调用");

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);

}

How do to all,

This is the first time posting on the site, so i presume i have not followed all the
formats and protocols correctly.

My app has a UIView containing a list of questions, and a UISegmentedControl called "question_btn" for each question. The UISegmentedControl has 2 segments, the first for a NO answer, the 2nd for a YES answer, corresponding to each question.

Each "question_btn" individually calls -(void) processQuestions{}, but when i click on each "question_btn" only the last one is processed properly. All previous "question_btns" call the processQuestions method, but do not implement its code.

Im my problem with the UISegmentControl, or the switch statement, or both.

Any suggestions would be greatly appreciated.

The button is defined as follows

 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]; 

Below is the processQuestions

- (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);
}

Here is how i got the code to iterate properly and get the question_btn selected index value. It was just a matter of creating a mutable array to hold the question_btn objects,
and then set the selected index value for each button using a for loop.
The buttonsArray is set in my createView method.

buttonsArray = [[NSMutableArray alloc] init];

- (void) processQuestions

{
//NSLog(@"QuestionsView processQuestions: Called");

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-10-07 11:00:28

如果您有问题列表,则可以使用分段控件数组。您可能有一个按钮“获取结果”,因此当按下时,您将遍历所有分段控件并检查所选索引(是/否 - 0/1)。

现在,在上面的代码中,为每个分段控件的状态更改调用 processQuestions 。如果用户保留默认值,则不会将结果添加到答案数组中。

If you have a list of questions, you can use an array of Segemnted control. You may have a button, 'Get Result' so when pressed, you iterate through all the segmented controls and check there selected index (yes/no - 0/1).

right now, in your above code, processQuestions is called for each segmented control's state changing. If the default value is kept by the user, it will not add result to the answer array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文