简体   繁体   English

将IBAction连接到自定义类

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM