0

Hi I have a quiz app that gives you a score in a UILabel after answering each question, so I have a button that when you hit the correct answer you get 10 points and if hit the wrong answer you get 0 points, but how do I bring the int score value to the next question, which is pushed by a navController.

Thanks!

1

5 Answers 5

1

You can declare an int property in the app delegate, and you can update or read it from anywhere in your application. You have no need to take the extra burden to carry the result from the first view controller all the way to the last view controller.

In app delegate declare a property named score.

And, in any of your view controllers,

YourAppDelegate *appDelegate; appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.score += 10; 
Sign up to request clarification or add additional context in comments.

3 Comments

hu, so if i do this I can just have my last view show my score instead of caring the score throughout the quiz?
Yes. You can increment the score from each view controller, if the user selects the correct answer.
How would that look like in code? I know how to increment and make an int property to the score, but how do I call the score in the questionViewControllers using the app delegate?
0

Well, there are a half-dozen ways to do it. Perhaps the least complicated for you would be to use your app delegate as a central data manager.

Comments

0

A more cleaner way is to pass that data when you push a new view controller:

UINextVC *vc = [[UINextVC alloc] initWithNibName:UINextVC andData:text]; //this'd be the label [self.navigationController pushViewController:vc]; [vc release]; 

Otherwise, do something like this before you push:

vc.data = text; 

3 Comments

sweet thx, yeah i thought there would be a more cleaner way to do this
This is one of the best ways. Otherwise another way is to have another object that handles all the data and have all other classes reference that object.
hmm I seem to get an error when I do this, not sure why, I imported "Question2ViewController" and used this in my IBActon method but I get an error:"expected expression before Question2ViewController"
0

Another way is to use static variable With a class method.

Comments

0

Another way is to use the NSNotificationCenter. This way, you can push and receive data / trigger events anywhere without having to pass them from class to class. Imagine it as a wireless transmission.

1 Comment

This is legit, but for my app it is just as easy as going through the app delegate, mainly because my app is not a game that needs to keep track of lives,score,level,etc.. So yeah I probably would want to use this for more intense apps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.