user3161041
user3161041

Reputation: 13

Passing a changing int value to other view controllers

I am very new in Xcode and I have of course faced a problem which most of us do.. I am trying to creating something like a "quiz" game for IPhone just to improve my 'skills'. I am facing a problem now which is that I have a variable for my first view controller that is called currentPoint, as the player receives an 'alert view' everytime he/she tries a guess for the quiz. However I have done the increment (meaning; currentPoint++) for the correct answer so that the 'alert view' shows the actual current point. Here is what my problem is:

When the second question is being answered and we get to the next view controller, my currentPoint variable turns down to 0 again. No matter how many times you have answered correctly in the entire quiz, you will end by 0 point. How do i transfer the previous currentPoint variable to the next view controller?

In Java you can for instance use the previous class by doing something like this:

previousClass a = new previousClass;

a.currentPoint++;

Thus the former currentPoint (lets say it was 3) has now got an increment so that it will be one higher (which is 4). How do I do this in Xcode (note that I am a beginner).

I hope that my question is to be understood and I hope that you guys will answer me in a way that is appropriate to me as I am a beginner (for instance tell me whether I should add things in the .h or .m files etc.)

I have looked around for this question for a long time, but I did not understand the answers as their case was not 100 % like my case, and that I am a beginner. I am very sorry if this looks - or is - like another question on this site.

Upvotes: 0

Views: 108

Answers (1)

Dinesh
Dinesh

Reputation: 2204

You have to use Segue to pass values between View Controllers. You have to use the following method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SegueIdentifier"]) {

        NextViewController *nextController = segue.destinationViewController;
        // add code to pass values to variables of nextController 
        nextController.scoreVariable = self.score;
    }
}

You can refer this for more details: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

Edit - If you have scoreVariable defined in the next View controller and score defined in the current view controller, you can use the code above.

Upvotes: 1

Related Questions