简体   繁体   中英

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!

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;

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.

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;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM