Ios 自定义UIView中的UIButton不';行不通
我有一个自定义UIView,当我的应用程序中发生错误时会显示它。它有一个关闭按钮(UIButton),当用户单击它时,它将关闭“显示视图的错误”。我现在面临的问题是,当用户单击关闭按钮并且错误视图未被删除时,在我的自定义视图中没有执行任何操作。我是否必须修改一些代码或需要添加一些代码才能使我的按钮操作在自定义视图中工作 这是我的代码片段:Ios 自定义UIView中的UIButton不';行不通,ios,swift,uiview,uibutton,Ios,Swift,Uiview,Uibutton,我有一个自定义UIView,当我的应用程序中发生错误时会显示它。它有一个关闭按钮(UIButton),当用户单击它时,它将关闭“显示视图的错误”。我现在面临的问题是,当用户单击关闭按钮并且错误视图未被删除时,在我的自定义视图中没有执行任何操作。我是否必须修改一些代码或需要添加一些代码才能使我的按钮操作在自定义视图中工作 这是我的代码片段: class GeneralErrorHandler: UIView { @IBOutlet weak var closeButtonOutlet: UIBu
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
}
}
我有一个调用GeneralErrorHandler的NSError扩展,我将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()
}
}
}
尝试启用用户与视图的交互。您可以发布您如何使用自定义视图吗?这是swift为什么要放置objective c标记??!?!!??!?您能发布代码吗?如何在viewController类中使用theView?@Frederico Novack Amaral Pereir:您确定您的代码在调用show()时显示GeneralErrorHandler吗?我面临崩溃,因为它导致无限递归调用init