1

我正在尝试使用prepareForSegue方法在另一个 VC 上设置一个整数。我有四个按钮,每个按钮在按下时都有自己的布尔值

- (IBAction)answerChoiceFourPressed:(id)sender {
    self.ButtonFourPressed = YES;
}

我在这里有这个方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ((SecondQuestionViewController *)segue.destinationViewController).delegate=self;

    if ([segue.identifier isEqualToString:@"toQuestion2"]) {
        SecondQuestionViewController *mdvc = segue.destinationViewController;

        NSInteger newInt;

        if (ButtonTwoPressed == YES) {
            newInt = 1;
            [mdvc setSecondScore:newInt];}

        if (ButtonThreePressed == YES) {
            newInt = 2;
            [mdvc setSecondScore:newInt];}

        if (ButtonFourPressed == YES) {
            newInt = 3;
            [mdvc setSecondScore:newInt];} 

        else {
            [mdvc setSecondScore:0];}
    }
}

在 SecondQuestionViewController 中,我已经涵盖了所有内容:

#import "FirstQuestionViewController.h"

并且 secondScore int 被声明为@property:

@property (nonatomic, assign) NSInteger secondScore;

我正在使用 NSLog(@"Score NUMBER 2 is %d", secondScore);

它总是给我0,除非按下第四个按钮:它给我3(prepareForSegue方法中的最后一个)可能的问题是什么?提前致谢!

4

3 回答 3

2

Your if statements are whacked. You're last condition was setting the value to 0. Also no need for the newInt var to be declared. Just do this:

if (ButtonTwoPressed == YES) {
    [mdvc setSecondScore:1];
}
else if (ButtonThreePressed == YES) {
    [mdvc setSecondScore:2];
}
else if (ButtonFourPressed == YES) {
     [mdvc setSecondScore:3];
} 
else {
    [mdvc setSecondScore:0];
}
于 2012-09-06T22:10:37.467 回答
1

用这个:

if (ButtonTwoPressed == YES) {
    newInt = 1;
    [mdvc setSecondScore:newInt];
}
else if (ButtonThreePressed == YES) {
    newInt = 2;
    [mdvc setSecondScore:newInt];
}
else if (ButtonFourPressed == YES) {
    newInt = 3;
    [mdvc setSecondScore:newInt];
} 
else {
    [mdvc setSecondScore:0];
}
于 2012-09-06T22:03:10.890 回答
1

试试这个:

if (ButtonTwoPressed == YES) {
    newInt = 1;
    [mdvc setSecondScore:newInt];}

else if (ButtonThreePressed == YES) {
    newInt = 2;
    [mdvc setSecondScore:newInt];}

else if (ButtonFourPressed == YES) {
    newInt = 3;
    [mdvc setSecondScore:newInt];} 

else {
    [mdvc setSecondScore:0];}

问题是你的 if 语句是分开的,所以当它到达最后一个并且没有按下按钮时,会调用“else”指令,并将值设置为 0。

于 2012-09-06T22:10:09.283 回答