[英]Connect IBAction to custom class
I wrote a custom class called PressableView
. 我编写了一个自定义类
PressableView
。 It recognizes taps and then calls a protocol function on its delegate object. 它识别出抽头,然后在其委托对象上调用协议功能。 Since it is a subclass of
UIView
, it does not allow the connection of IBAction
s by default. 由于它是
UIView
的子类,因此默认情况下不允许IBAction
的连接。
I was wondering whether it is possible to connect a function inside the view controller, the pressable view is in, to the object, so it calls that method on tap – just like you would with a UIButton
. 我想知道是否可以在视图控制器内部连接一个函数(该对象位于可按下的视图中),因此它可以在调用时调用该方法,就像使用
UIButton
。
I already tried things like: 我已经尝试过类似的事情:
class PressableView: UIView {
// ...
@IBOutlet var action: (() -> Void)?
// ...
}
...but, Xcode doesn't allow that type to be an IBOutlet
. ...但是,Xcode不允许该类型为
IBOutlet
。
Any ideas? 有任何想法吗?
change PressableView
parent class as UIControl
class then you can connect for actions and handle it. 将
PressableView
父类更改为UIControl
类,然后可以进行连接并对其进行处理。
UIControl
is subclass of UIView
class only. UIControl
仅是UIView
类的子类。 so you will have all the properties of UIView
as well. 因此,您还将拥有
UIView
所有属性。
class PressableView: UIControl {
@IBAction func uicontrolEventAction(_ sender: Any) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.