iOS 自定义控件
我正在构建一个自定义控件,如下图所示。
它基本上是一个带有滑块的菜单。箭头将允许我更改滑块轨道上的三天显示。根据我使用滑块选择的日期,我想更改主屏幕上的一些视图(此菜单将位于页面底部)。所以基本上这是我在主控制器中“监听”的唯一事情:如果选择了某一天。
我已经弄清楚了我必须编写的所有代码,但我不确定是否应该子类化 UIControl 或 UIView。如果是这样,我应该在 UIControl (UIView) 中的哪里编写控制器的代码(更改日期、添加拖动效果等)?或者我应该子类化 UIViewController,并在那里编写所有代码。 (但如果是这样,为什么我应该以第一种方式子类化 UIControl (UIView) )。
所以基本上我想知道除了我的自定义控件的视图界面(我在 IB 中所做的)之外,我还需要创建哪些额外文件,我应该在哪里放置代码(IBOutlets、IBAction 方法)以及如何与主视图控制器(我将主控制器设置为自定义控件的委托?)。
抱歉发了这么长的帖子。
谢谢
I am building a custom control that looks like the one in the image below.
It will basically be a menu with a slider. The arrows will allow me to change the three days show on the slider track. Acoording to the day I select with the slider I want to change some views on the main screen (this menu will on the bottom of my page). So basically this is the only thing that I will "listen" in my main controller: if some day has been selected.
I have figured out all the code I will have to write, but I am not sure wether I should subclass UIControl or UIView. And if so, where should I write the code of my controller (changing the days, adding the drag effec, etc ) in the UIControl (UIView)? Or should I subclass UIViewController, and write there all the code there. (but if so, why should I subclass UIControl (UIView) in the first way).
So basically I want to know what extra files I need to create, besides the view interface of my custom control(which I did in the IB), where should I put the code (IBOutlets, IBAction methods) and how do I communicate with the main view controller (I set the main controller as a delegate of my custom control?).
Sorry for the long post.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议对
UIControl
进行子类化。此控件的用户可以执行[yourControl addTarget:self action:@selector(someMethod:) forControlEvents:UIControlValueChanged];
对更改的值做出反应。在您的控件中,当您确定已选择新的一天时,您可以调用[self sendActionsForControlsEvents:UIControlValueChanged];
,瞧,所有感兴趣的类都会收到通知。尽可能保持该控件的独立性。这意味着,只需为其提供所需的逻辑即可,仅此而已。想想你如何使用 Apple 提供的 UI 元素:尝试使你的元素变得通用(如果可行的话;在这里使用常识)。简而言之:您应该努力使此控件足够通用,以便它在其他项目或应用程序的其他位置对您有用。
I recommend to subclass
UIControl
. Users of this control can do[yourControl addTarget:self action:@selector(someMethod:) forControlEvents:UIControlValueChanged];
to react to changed values. In your control, when you have determined that a new day has been selected, you call[self sendActionsForControlsEvents:UIControlValueChanged];
and voila, all interested classes will get informed.Keep that control as self-contained as possible. This means, only give it as much logic as you need to, and nothing more. Think about how you use Apple provided UI elements: try to make yours as generic (if practical; use common sense here). In short: you should thrive to make this control generic enough that it could be useful to you in other projects or other places of your app.
简短的答案是您应该子类
UIControl
并将绘制组件并与组件交互的所有逻辑放在那里。UIControl
继承自UIView
并添加目标/操作行为。这样,只要日期发生变化,您就可以使用UIControlEventValueChanged
来sendAction:to:forEvents:
。您也可以在用户更改所选日期时实现委托协议。
例如:
您不想使用 UIViewController,因为它的工作是管理更高级别的视图,例如您的小部件所在的屏幕。稍后,当您使用组件并执行设置最初显示的日期和侦听更改事件等操作时,您将使用视图控制器。
The short answer is you should subclass
UIControl
and put all of the logic to draw the component and interact with the component there.UIControl
inherits fromUIView
and adds target/action behavior. This way you cansendAction:to:forEvents:
withUIControlEventValueChanged
whenever the date changes.You could alternatively implement a delegate protocol for when the user changes the selected date.
For example:
You don't want to use a
UIViewController
since it's job is to manage higher-level views, like the screen that your widget is on. You'll use a view controller later when you are consuming your component and do things like set the date to display initially and listen for change events.