将 UIView 或 UIViewController 用于计时器对象? [英] To use a UIView or UIViewController for timer object?

查看:28
本文介绍了将 UIView 或 UIViewController 用于计时器对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个元素的 UI,我想将其中一些元素封装到它们自己的对象中.例如,一个对象将是一个带有外部可用成员的计时器(基本上只是一个位于 UIView 中的 UILabel):

I've got a UI with several elements, some of which I'd like to encapsulate into their own objects. For example, one object would be a timer (basically just a UILabel sitting in a UIView) with the externally-available members:

  • startTime(属性)
  • startpause(方法)
  • startTime (property)
  • start and pause (methods)

(当计时器到达 0 时,还有一个 im_done NSNotification.)这个对象和其他几个对象将在一个 UIViewController 中一起使用.

(And also a im_done NSNotification when the timer reaches 0.) This object and several others would be used together in a single UIViewController.

UIView 或 UIViewController 是否更适合作为计时器对象(以及所有其他对象)的子类?阅读 这个答案 让我相信 UIView,但对于我的具体情况(尤其是对于更复杂的对象),我不确定.感谢阅读.

Would a UIView or UIViewController be more appropriate to subclass for the timer object (and all the others)? Reading this answer leads me to believe UIView, but for my specific case (especially for the more complicated objects), I'm not sure. Thanks for reading.

只是为了澄清,我希望所有实现计时器功能的代码与我的主视图控制器分开.一个重要的原因是计时器对象是可移植的.

Just to clarify, I would want all code that implements timer functionality separate from my main view controller. One big reason is so that the timer object would be portable.

推荐答案

你应该有一个 UIView 的子类,但它应该只有视图组件来显示你需要的任何时间信息.您还应该有一个 MyTimer 类,该类封装了计时功能并提供对更改的回调(例如剩余时间,或者当剩余时间达到零时作为单独的完成).理想情况下,回调传递 MyTimer 以及为方便起见,剩余时间值,因此您无需在简单用例中使用访问器方法).

You should have a subclass of UIView, but it should just have the view components to display whatever time information you need. You should also have a MyTimer class which encapsulates the timing functionality and provides callbacks on changes (such as the time remaining and perhaps the completion as a separate when the time remaining reaches zero). Ideally the callbacks pass the MyTimer and, as a convenience, the remaining time value so you don't need to use the accessor method in the simple use case).

现在,您已经拥有一个管理您更广泛视图的视图控制器,它可以创建将您的 UIView 子类添加到主视图并创建、拥有和管理关联的 MyTimer 驱动这些视图的实例.

Now, you already have a view controller which is managing your broader view, and it can create add your UIView subclass to the main view and create, own and manage the associated MyTimer instances which drive those views.

这篇关于将 UIView 或 UIViewController 用于计时器对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆