制作我自己的UIControlEvent并触发它? [英] Make my own UIControlEvent, and trigger it?
问题描述
我有一个自定义的UIView类,它是在viewcontroller中创建的.我想让该类可重用,所以不想使其仅与此1个viewcontroller一起使用.因此,我认为告诉我的视图控制器用户已与该类进行交互的最佳方法是以某种方式创建我自己的UIControlEvent.也许像这样:
I have a custom class of a UIView I've made inside my viewcontroller. I want to make this class re-usable and so don't want to build it to work just with this 1 viewcontroller. Because of this, I think the best way of telling my viewcontroller that the user has interacted with the class is to somehow make my own UIControlEvent. Maybe something like:
[customClass addTarget:self action:@selector(whatIWantToHappen) forControlEvents:UIControlEventWhatever];
我对此并不了解,对此有什么建议吗?
I don't know much about doing this though, any suggestions on this?
推荐答案
您可以通过对UIControl
进行子类化来实现此目的,该子类继承自UIView,但提供了一些用于处理目标和操作的其他方法.看看
You could do this by subclassing UIControl
, which inherits from UIView, but provides some additional methods for handling targets and actions. Have a look at the constants, perhaps one of the predefined control events already fits your bill. Otherwise, you could define your own events in the range provided by UIControlEventApplicationReserved
, you should however never prefix your own things with UI...
, that 'namespace' is reserved for UIKit.
这篇关于制作我自己的UIControlEvent并触发它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!