自定义UIView内的UIButton不起作用

时间:2017-09-15 15:20:02

标签: ios swift uiview uibutton

我的应用程序中发生了一些错误时出现的自定义UIView。它有一个关闭按钮(UIButton),用户点击它时将关闭“错误显示视图”。我现在面临的问题是,当用户单击关闭按钮并且未删除错误视图时,我的自定义视图中没有执行任何操作。我是否必须修改某些代码或者需要添加一些代码才能使我的按钮操作在自定义视图中工作?

以下是我的代码段

class GeneralErrorHandler: UIView {

@IBOutlet weak var closeButtonOutlet: UIButton!
@IBOutlet weak var errorTxtLbl: UILabel!
@IBOutlet weak var contentView: UIView!

let theView:UIView

init(errorText:String, view:UIView, position:CGPoint){
    theView = view
    super.init(frame:CGRect(x: position.x, y: position.y, width: UIScreen.main.bounds.width, height: 62))

    Bundle(for: type(of: self)).loadNibNamed("GeneralErrorView", owner: self, options: nil)

    contentView.frame = self.frame
    errorTxtLbl.text = errorText

    closeButtonOutlet.addTarget(self, action: #selector(forceCloseButton), for: .touchUpInside)
}

func show(){
    theView.addSubview(contentView)
}

required init?(coder aDecoder: NSCoder) {
    theView = UIView()
    super.init(coder: aDecoder)
    Bundle(for: type(of: self)).loadNibNamed("GeneralErrorView", owner: self, options: nil)
    closeButtonOutlet.addTarget(self, action: #selector(forceCloseButton), for: .touchUpInside)
}

func forceCloseButton(){
    contentView.removeFromSuperview()
    self.removeFromSuperview()
}

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.frame = self.bounds
}
}

我有一个NSError扩展,调用GeneralErrorHandler并传递ViewController的视图,我想要出现错误

extension NSError {

    func showErrorScreen(view:UIView, position:CGPoint = CGPoint.zero){

        if isInternetAvailable() {
            print("Inernet is availble, another error happened")
            GeneralErrorHandler(errorText: self.errorMessage(), view: view, position: position).show()

        }else{
            print("Internet not available")
            ConnectionError(view: view, position:position).show()
        }

    }
}

0 个答案:

没有答案