如何实现自定义控件的目标操作机制? [英] How to implement target-action-mechanism for custom control?

查看:138
本文介绍了如何实现自定义控件的目标操作机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要写我自己的自定义控件,这是非常不同于UIButton。这是非常不同的,我决定从头开始写。所以我的子类是UIControl。

I'm going to write my own custom control that is very different from UIButton. It's so much different that I decided to write it from scratch. So all I subclass is UIControl.

当我的控件被触摸里面,然后我想发送一个消息的目标行动的手段。该类的用户可以实例化它,然后为此事件添加一些目标和操作。

When my control is touched up inside, then I want to fire a message in means of target-action. The user of that class may instantiate it and then add some targets and actions for this event.

想象我会在内部调用一个方法-fireTargetsForTouchUpEvent。我如何在我的课上维护这个目标 - 行动机制?我必须将所有目标和动作添加到我自己的数组,然后在for循环中调用目标对象上的选择器(动作)。或者有更聪明的方法吗?

i.e. imagine I would call internally a method -fireTargetsForTouchUpEvent. How could I maintain this target-action-mechanism in my class? Do I have to add all targets and actions to my own array and then just call selectors (the actions) on the target objects in a for-loop? Or is there a more intelligent way to do it?

我想为某些事件添加目标和动作的方法,比如touch up事件通过在发生时调用内部方法)。任何想法?

I imagine to provide some methods for adding targets and actions for some events like that touch up event (I raise that manually by calling a internal method when that happens). Any idea?

推荐答案

由于你计划子类化UIControl,你可以使用

Since you're planning to subclass UIControl, you can just use

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

使用这个,任何类都可以注册自己作为任何事件的目标, 。

Using this, any class can register itself as a target for any events it wants to on your custom controller.

这篇关于如何实现自定义控件的目标操作机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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