问题描述
|
我在Viewcontroller中创建了UIView的自定义类。我想使此类可重用,所以不想使其仅与此1个viewcontroller一起使用。因此,我认为告诉我的视图控制器用户已与该类进行交互的最佳方法是以某种方式创建我自己的UIControlEvent。也许像这样:
[customClass addTarget:self action:@selector(whatIWantToHappen) forControlEvents:UIControlEventWhatever];
我对此并不了解,对此有什么建议吗?
解决方法
您可以通过继承
UIControl
来实现此目的,UIControl
继承自UIView,但提供了一些其他方法来处理目标和操作。看一下常量,也许预定义的控制事件之一已经符合您的要求。否则,您可以在UIControlEventApplicationReserved
提供的范围内定义自己的事件,但是,切勿在things3ѭ之前添加自己的事件,因为\'namespace \'是为UIKit保留的。
,除了omz的答案之外,执行此操作时可能还会收到编译器警告:
[self addTarget:self action:@selector(selector:) forControlEvents:CustomControlEvent];
编译器不喜欢最后一个参数使用的自定义值(类型为UIControlEvents
),因此会发出警告。
我这样做:
enum CustomControlEvent : UIControlEvents
{
CustomControlEventWHATEVER = UIControlEventApplicationReserved
};
typedef enum CustomControlEvent CustomControlEvent;
看哪!没有更多警告。
我在这个StackOverflow答案中找到了这个符号。
N.B.对于UIControlEvents
,我强烈避免使用任何不可用的值,因此我仅使用UIControlEventApplicationReserved
值。
此外,typedef放弃了每次都键入“ enum”的需求,这是惯例。