-4

如下图所示,我要求用户选择他们在日常生活中进行的活动。

我想以字符串的方式获取这些信息并将其发送到我的数据库。

我想出了一个方案来确定:

步行:1,骑自行车:2,远足:3,足球:4,其他:5,无:0

描述左侧显示的每个图像都是一个带有 iBOutlet 的按钮(IBAction)。

例如,如果用户只选择步行、远足和足球,我的字符串将是“134”

我怎么可能达到?

- (IBAction)walkingBtnAction:(id)sender {
    counter1++;
    if(counter1%2==0)
    {
        [walkingBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }

    if(counter1%2==1)
    {
        [walkingBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }

    //[self.view addSubview:myButton];
}

- (IBAction)bikingBtnAction:(id)sender {
    counter2++;
    if(counter2%2==0)
    {
        [bikingBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }

    if(counter2%2==1)
    {

        [bikingBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }

}

- (IBAction)hikingBtnAction:(id)sender {
    counter3++;

    if(counter3%2==0)
    {
        [hikingBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }

    if(counter3%2==1)
    {
        [hikingBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }
}

- (IBAction)soccerBtnAction:(id)sender {
    counter4++;

    if(counter4%2==0)
    {
        [soccerBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }
    if(counter4%2==1)
    {
        [soccerBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }
}

- (IBAction)othersBtnAction:(id)sender {
    counter5++;
    if(counter5%2==0)
    {
        [othersBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }
    if(counter5%2==1)
    {
        [othersBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }

}

- (IBAction)noneBtnActions:(id)sender {
    counter6++;
    if(counter6%2==0)
    {
        [noneBtnOutlet setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    }
    if(counter6%2==1)
    {
        [noneBtnOutlet setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
    }

}

在此处输入图像描述

4

2 回答 2

2
// maybe declare this as a class variable in the .h file
NSString finalString = @"";

- (IBAction)walkingBtnAction:(id)sender {
    if(walkingClicked)
        finalString = [finalString stringByAppendingString:@"1"];
    else
        finalString = [finalString stringByReplacingOccurancesOfString:@"1" withString:@""];
}

现在你必须为你的所有行动都这样做

PS:代码未测试

于 2013-07-15T22:36:33.367 回答
1

你为什么不在那个页面上创建一个名为选项的 iVar(例如)然后这样做

[choices stringByAppendingString:@"numberyouwant"];
于 2013-07-15T22:36:34.770 回答