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!

Azhar
  • 20,500
  • 38
  • 146
  • 211
mat
  • 41
  • 1
  • 4
  • possible duplicate of [How to update or send an int to the proceeding view controller](http://stackoverflow.com/questions/6618616/how-to-update-or-send-an-int-to-the-proceeding-view-controller) – jscs Jul 08 '11 at 07:11

5 Answers5

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;
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • hu, so if i do this I can just have my last view show my score instead of caring the score throughout the quiz? – mat Jul 08 '11 at 02:08
  • Yes. You can increment the score from each view controller, if the user selects the correct answer. – EmptyStack Jul 08 '11 at 03:22
  • 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? – mat Jul 08 '11 at 04:03
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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
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;
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • sweet thx, yeah i thought there would be a more cleaner way to do this – mat Jul 08 '11 at 01:46
  • 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. – Enrico Susatyo Jul 08 '11 at 01:48
  • 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" – mat Jul 08 '11 at 02:01
0

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

user523234
  • 14,323
  • 10
  • 62
  • 102
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.

Community
  • 1
  • 1
Manny
  • 6,277
  • 3
  • 31
  • 45
  • 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 – mat Jul 08 '11 at 04:52