Reputation: 41
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!
Upvotes: 0
Views: 827
Reputation: 6287
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.
Upvotes: 0
Reputation: 51374
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;
Upvotes: 1
Reputation: 19790
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;
Upvotes: 0
Reputation: 47699
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.
Upvotes: 0