更新另一个视图控制器中的 UILabel

发布于 2024-11-19 06:52:03 字数 157 浏览 3 评论 0原文

你好,我有一个测验应用程序,在回答每个问题后,它会在 UILabel 中给你一个分数,所以我有一个按钮,当你回答正确的答案时,你会得到 10 分,如果回答错误的答案,你会得到 0 分,但是我该如何做将 int 分数值带入下一个问题,该问题由 navController 推送。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过气美图社 2024-11-26 06:52:03

您可以在应用程序委托中声明int 属性,并且您可以从应用程序中的任何位置更新或读取它。您无需承担额外的负担将结果从第一个视图控制器一直传送到最后一个视图控制器。

在应用委托中声明一个名为 score属性

并且,在任何视图控制器中,

YourAppDelegate *appDelegate;
appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.score += 10;

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;
贪恋 2024-11-26 06:52:03

嗯,有六种方法可以做到这一点。也许对您来说最简单的方法是使用您的应用程序委托作为中央数据管理器。

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.

Oo萌小芽oO 2024-11-26 06:52:03

更简洁的方法是在推送新视图控制器时传递该数据:

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

否则,在推送之前执行类似的操作:

vc.data = text;

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;
豆芽 2024-11-26 06:52:03

另一种方法是将静态变量与类方法一起使用。

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

不及他 2024-11-26 06:52:03

另一种方法是使用 NSNotificationCenter。这样,您可以在任何地方推送和接收数据/触发事件,而无需在类之间传递它们。将其想象为无线传输。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文