子视图UIButton单击事件无法从ViewController迅速4

时间:2018-10-10 11:49:45

标签: ios swift uibutton

我的自定义EmptyUIView。我想从UIView调用UIViewController 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")
    }
}

1 个答案:

答案 0 :(得分:1)

您已禁用与视图的userInteraction,当您禁用用户交互时,它禁用了与其所有子视图的交互更改

view.isUserInteractionEnabled = false

使用

view.isUserInteractionEnabled = true