调用方法后,文本字段应立即更新
我正在从事一个涉及试卷的项目。我在视图上一次仅显示 1 个问题。用户回答问题后,如果用户向左侧滑动,则会显示第二个问题。
我放置了一个文本字段来显示每个时间点的分数。我实现了它,但只有当用户导航到下一个问题时我的分数才会更新。 我的要求是,一旦问题得到解答,分数就应该在文本字段中更新。
scoreField.text=[NSString stringWithFormat:@"%d",currentScore];
有什么技巧可以在回答问题时这样做吗?我的论文有 20 个问题和 20 个提交按钮,因此我无法将上述代码放置在每个提交按钮操作方法中。这将是丑陋且无效的编程。
如果有什么办法可以解决我的情况,请帮忙。
提前致谢
I am working on a project which deals with an examination paper. I display only 1 question at a time on the view. After the users answers the question a second question is displayed if the user swipes towards left hand side.
I have placed a textField to display the score at each point in time. I implemented it but my score gets updated only if the user navigates to the next question.
My requirement is that as soon as the question is answered the score should be updated in the textField.
scoreField.text=[NSString stringWithFormat:@"%d",currentScore];
Is there any technique to do so whenever a question is answered? My paper has 20 questions and has 20 submit buttons so I cannot place the above code at each and every submit button action method. It would be ugly and not effective programming.
Please help if there is any way to solve my case.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正确理解您的问题,那么您不想在每个提交按钮处理程序中设置
scoreField.text
。我假设您在导航到下一个问题时设置了此项。您只需在currentScore
更改时更新此设置。所以我认为最好为currentScore
创建一个 setter 并从中更新scoreField.text
。像这样:每当您需要更改分数时,调用
setCurrentScore
。或者更好的是,您可以使用 setter 属性并编写自己的 setter 实现。If you understand your problem correctly then you don't want to set
scoreField.text
in every submit button handler. I am assuming that you are setting this when navigating to next question. You only need to update this whencurrentScore
is changed. So I think it's better to create a setter forcurrentScore
and updatescoreField.text
from that. Something like this:And call
setCurrentScore
whenever you need to change the score. Or even better, you can use a setter property and write your own setter implementation.这可以变得非常简单、优雅。有一些假设:
+[Examination sharedExammination]
。score
。UILabel
的自定义子类来显示分数。有了这些假设,您就可以让自定义
UILabel
注册对score
属性的 KVO 更改并自动更新自身。UILabel
子类的实现将包括如下内容:This can be made really simple, and elegant. With a few assumptions:
+[Examination sharedExammination]
.score
.UILabel
to display the score.With these assumptions you can let your custom
UILabel
register for KVO changes to to thescore
property and update itself automatically. The implementation of theUILabel
subclass would include something like this:试试这个:
Try this: