另一个VC上的'setInt'无效

时间:2012-09-06 21:58:42

标签: iphone objective-c ios xcode nsinteger

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

- (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方法中的最后一个) 有什么问题可能? 提前谢谢!

3 个答案:

答案 0 :(得分:2)

你的if语句被打破了。你最后的条件是将值设置为0.也不需要声明newInt var。就这样做:

if (ButtonTwoPressed == YES) {
    [mdvc setSecondScore:1];
}
else if (ButtonThreePressed == YES) {
    [mdvc setSecondScore:2];
}
else if (ButtonFourPressed == YES) {
     [mdvc setSecondScore:3];
} 
else {
    [mdvc setSecondScore:0];
}

答案 1 :(得分: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];
}

答案 2 :(得分: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.