我正在创建自定义 UIView
。我想知道如何在此 UIView
上复制 UIButton
行为。
我希望能够像 UIButton
一样调用 addTarget
。
我知道我可以继承 UIControl
并调用
self.sendActionsForControlEvents(UIControlEvents.TouchUpInside);
但我想在 UIView
上进行,因为我是在 XIB 文件上设计的。
最佳答案
有一种简单的方法可以实现这一点。
- Go to Interface builder of your class
- Select the view u want to add action
- open IDENTITY INSPECTOR, change class from UIView to 'UIControl'.
现在您可以向该 View 添加任何 IBAction 方法。
它的工作就像魅力一样。 快乐的编码。 希望对你有帮助
更新
its working in xCode 10.1 and swift 4+
关于ios - 自定义 UIView 添加目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33472080/