mat
mat

Reputation: 41

update UILabel in another view controller

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

Answers (5)

Manny
Manny

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

EmptyStack
EmptyStack

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

user523234
user523234

Reputation: 14834

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

Upvotes: 0

Enrico Susatyo
Enrico Susatyo

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

Hot Licks
Hot Licks

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

Related Questions