当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - 'setInt' on another VC not working

问题描述:

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!

网友答案:

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];
}
网友答案:

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.

网友答案:

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];
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: