1

I'm trying to set an Integer on another VC using prepareForSeguemethod. I've got four buttons and each button has its own boolean value changed when pressed

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

I have got this method here:

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

In SecondQuestionViewController I've got it all covered:

#import "FirstQuestionViewController.h"

and the secondScore int is declared as a @property:

@property (nonatomic, assign) NSInteger secondScore;

I'm using NSLog (@"Score NUMBER 2 is %d", secondScore);

And it's always giving me 0, unless the fourth button is pressed: it gives me 3 (last one in prepareForSegue method) What is the possible problem? Thanks in advance!

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120

3 Answers3

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];
}
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
1

use this:

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];
}
Kyle
  • 434
  • 2
  • 10
1

Try this:

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

The problem is that your if statements are separated, so when it reach the last one and button for was not pressed, the "else" instructions are called, and the value is set to 0.

Garoal
  • 2,364
  • 2
  • 19
  • 31