简单时钟应用程序:每秒使用委托还是回调?

发布于 2024-12-11 06:12:31 字数 486 浏览 2 评论 0原文

我对 Objective c 很感兴趣。作为一个简单的程序,我想编写一个时钟应用程序。

基本上,UITextField 需要显示当前时间并每秒更新。

我最初的想法是使用委托并让 UITextField 在“值更改”事件发生时回调一个类。通过“引导”初始值更改(例如,通过设置应用程序启动时的时间),我想我可以在之后连续触发“值更改”事件(UITextField 将不断更改自身,从而触发委托方法)。我尝试了很多事情,但这从来没有奏效。我什至尝试创建一个按钮,将 UITextField 设置为任意文本值(而不是在启动时设置 UITextField),希望调用委托方法,但这也没有。为了证明我的代码是正确的,当我使用“Touch Down”等其他操作时,时间会被更新:例如,我会在 UITextField 中每次单击时获取时间。

我最终发现我可以通过使用 [self PerformSelector ...] 每秒使用一次回调,并且这有效。

我国代表团使用“价值改变”行动从未奏效是否有根本原因?

I'm having some fun with objective c. As a simple program I wanted to write a clock application.

Basically, a UITextField needs to show the current time and update every second.

My initial thought was to use delegation and let UITextField call back a class when the 'Value Changed' event occurs. By 'bootstrapping' an initial value change (e.g. by setting the time at application startup) I thought I could trigger the 'Value Changed' event continuously afterwards (the UITextField would continuously change itself, hence triggering the delegate method). I tried many things, but this never worked. I even tried creating a button that would set UITextField to an arbitrary text value (as opposed to setting UITextField at startup) in the hope that the delegated method would be called, but this did not either. To prove that my code was correct, the time was updated when I'd use other actions like 'Touch Down' for example: I would get the time on every click in the UITextField.

I eventually found out that i could use a callback every second by using [self performSelector ...] and that worked.

Is there a fundamental reason my delegation using the 'Value Changed' action never worked ?

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

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

发布评论

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

评论(1

谜兔 2024-12-18 06:12:31

值更改事件仅在响应用户事件时触发 - 也就是说,根据设计,您设置 textField.text = "something" 不会触发它。

这是一件好事,但事实并非如此,因为从听起来你试图让你的应用程序进入无限循环。如果当您在框中设置文本时确实触发了“值更改”事件,则程序将再次询问委托人,这将再次设置文本,这将再次询问委托人......您明白了。这称为无限循环,它会导致程序挂起,然后崩溃,因为程序执行无法退出此循环。

不管怎样,为了做到你所说的,你有两个选择

  1. 你可以设置一个 NSTimer 对象来每秒调用你的时间更新方法。这很简单,查看 文档

  2. performSelector:withObject:afterDelay:。听起来您可能已经掌握了这一点。它不像使用 NSTimer 那么简洁,但它可以完成这项工作。

The value changed event only fires in response to a user event-- that is, you setting your textField.text = "something" doesn't fire it, by design.

And it's a good job it doesn't, because by the sounds of it you were trying to get your application into an infinite loop. If the 'value changed' event did actually fire when you set the text in the box, the program would ask the delegate again, which would set the text again, which would ask the delegate again..... you get the picture. This is called an infinite loop, and it has the effect of causing the program to hang, and then crash, since there's no way for the program execution to exit this loop.

Anyway, in order to do what you're saying, you've got two options

  1. you can set up an NSTimer object to call your time update method every second. It's quite easy, check out the documentation.

  2. performSelector:withObject:afterDelay:. It sounds like you might have already got the hang of this one. It's not as neat as using an NSTimer, but it will do the job.

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