更新另一个视图控制器中的 UILabel
你好,我有一个测验应用程序,在回答每个问题后,它会在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在应用程序委托中声明int 属性,并且您可以从应用程序中的任何位置更新或读取它。您无需承担额外的负担将结果从第一个视图控制器一直传送到最后一个视图控制器。
在应用委托中声明一个名为 score 的属性。
并且,在任何视图控制器中,
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,
嗯,有六种方法可以做到这一点。也许对您来说最简单的方法是使用您的应用程序委托作为中央数据管理器。
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:
Otherwise, do something like this before you push:
另一种方法是将静态变量与类方法一起使用。
Another way is to use static variable With a class method.
另一种方法是使用 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.