调用方法后,文本字段应立即更新

发布于 2024-11-06 22:31:52 字数 377 浏览 0 评论 0原文

我正在从事一个涉及试卷的项目。我在视图上一次仅显示 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 技术交流群。

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

发布评论

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

评论(3

余生共白头 2024-11-13 22:31:52

如果您正确理解您的问题,那么您不想在每个提交按钮处理程序中设置 scoreField.text 。我假设您在导航到下一个问题时设置了此项。您只需在 currentScore 更改时更新此设置。所以我认为最好为 currentScore 创建一个 setter 并从中更新 scoreField.text 。像这样:

- (void)setCurrentScore:(NSInteger)newScore {
    currentScore = newScore;
    scoreField.text=[NSString stringWithFormat:@"%d",currentScore];
}

每当您需要更改分数时,调用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 when currentScore is changed. So I think it's better to create a setter for currentScore and update scoreField.text from that. Something like this:

- (void)setCurrentScore:(NSInteger)newScore {
    currentScore = newScore;
    scoreField.text=[NSString stringWithFormat:@"%d",currentScore];
}

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.

还在原地等你 2024-11-13 22:31:52

这可以变得非常简单、优雅。有一些假设:

  • 存在保存分数的全局可访问模型对象,例如 +[Examination sharedExammination]
  • 模型对象具有 KVO(键值观察)兼容属性,例如 score
  • 您使用 UILabel 的自定义子类来显示分数。

有了这些假设,您就可以让自定义 UILabel 注册对 score 属性的 KVO 更改并自动更新自身。 UILabel 子类的实现将包括如下内容:

-(id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        [[Examination sharedExamination] addObserver:self
                                          forKeyPath:@"currentScore"
                                             options:0
                                             context:NULL];
    }
    return self;
}
-(void)dealloc 
{
    [[Examination sharedExamination] removeObserver:self
                                         forKeyPath:@"currentScore"];
    [super dealloc];
}
-(void)observeValueForKeyPath:(NSString*)keyPath 
                     ofObject:(id)object 
                       change:(NSDictionary*)change 
                      context:(void*)context
{
    if ([keyPath isEqualToString:@"currentScore"]) {
        scoreField.text = [NSString stringWithFormat:
                           @"Score: %d", [object currentScore]];
    } else {
        [super observerValueForKeyPath:keyPath
               ofObject:object
                 change:change
                context:context];
    }
}

This can be made really simple, and elegant. With a few assumptions:

  • There is globally accessible model object that holds the score, for example +[Examination sharedExammination].
  • The model object has a KVO (Key-Value-Observing) compatible property like score.
  • You use a custom subclass of UILabel to display the score.

With these assumptions you can let your custom UILabel register for KVO changes to to the score property and update itself automatically. The implementation of the UILabel subclass would include something like this:

-(id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        [[Examination sharedExamination] addObserver:self
                                          forKeyPath:@"currentScore"
                                             options:0
                                             context:NULL];
    }
    return self;
}
-(void)dealloc 
{
    [[Examination sharedExamination] removeObserver:self
                                         forKeyPath:@"currentScore"];
    [super dealloc];
}
-(void)observeValueForKeyPath:(NSString*)keyPath 
                     ofObject:(id)object 
                       change:(NSDictionary*)change 
                      context:(void*)context
{
    if ([keyPath isEqualToString:@"currentScore"]) {
        scoreField.text = [NSString stringWithFormat:
                           @"Score: %d", [object currentScore]];
    } else {
        [super observerValueForKeyPath:keyPath
               ofObject:object
                 change:change
                context:context];
    }
}
逐鹿 2024-11-13 22:31:52

试试这个:

[self.view setNeedsDisplay];

Try this:

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