ios - subview UIButton 单击事件在 ViewController swift 4 中不起作用

标签 ios swift uibutton

我的自定义 EmptyUIView。我想从 UIViewController 调用 UIView uiBtnExplore。

class EmptyView: UIView {
    @IBOutlet weak var uiBtnExplore: UIButton!

    class func createEmptyView() -> EmptyView {
        let myEmptyNib = UINib(nibName: "EmptyView", bundle: nil)
        return myEmptyNib.instantiate(withOwner: nil, options: nil)[0] as! EmptyView
    }
}

我的 UIViewController:

import UIKit
import XLPagerTabStrip

class PlaylistViewController: UIViewController, IndicatorInfoProvider {
    var itemInfo: IndicatorInfo = "PLAYLISTS"
    var emptyView:EmptyView!

    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return itemInfo
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        emptyView = EmptyView.createEmptyView()
        self.emptyView.center = CGPoint(x: view.frame.size.width  / 2,
                                        y: view.frame.size.height / 2)
        self.view.addSubview(emptyView)
        self.view.bringSubviewToFront(testButton)
        view.isUserInteractionEnabled = false

        self.emptyView.uiBtnExplore.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    }

    @objc func buttonAction(sender: UIButton!) {
        print("Button Clicked")
    }
}

最佳答案

您已禁用用户与您的 View 的交互,当您禁用用户交互时,它会禁用与其所有 subview 的交互,更改如下

view.isUserInteractionEnabled = false

view.isUserInteractionEnabled = true

关于ios - subview UIButton 单击事件在 ViewController swift 4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52739590/

相关文章:

ios - Siri Kit(语音转文本)禁用我的 TTS(文本转语音)iOS

ios - 为什么类名附加了 _private.h 字样

ios - 识别 UIViewController 上的 View

swift - UIButton setTitle 不起作用

ios - UIButton 文本出现一段时间,它在 ios 中显示另一个空文本?

ios - Firebase Performance 在第一次调用 NSURLSession 时导致应用程序崩溃

ios - 在不重新加载的情况下为 UITableViewCell subview 中的帧更改设置动画的最佳方法

ios - Swift 和 Xcode9 中的 VisualEffectView

ios - 使用 RSBarcodes 扫描条形码时执行操作

ios - 点击时 UIButton 图像不会改变