我有一个UIView,其中包含一个按钮和一些用于指示成功和失败的视图。我正在尝试在其他视图控制器上使用该UIView,并在被称为视图控制器上接收按钮操作。
这是我到目前为止尝试过的
protocol FailViewDelegate: class {
func tryAgainTapped()
}
class AlertView: UIView {
static let instance = AlertView()
weak var delegate : FailViewDelegate?
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var messageLbl: UILabel!
@IBOutlet weak var dashIMageView: AnimatableImageView!
@IBOutlet weak var circleView: AnimatableView!
@IBOutlet weak var iconStatus: AnimatableImageView!
@IBOutlet weak var tryAgainButton: AnimatableButton!
@IBOutlet weak var parentView: UIView!
private override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("AlertView", owner: self, options: nil)
}
enum AlertType {
case success
case failure
}
func showAlert(alertType: AlertType, to: UIViewController) {
switch alertType {
case .success:
dashIMageView.image = UIImage(named: "circle-dash-blue")
circleView.backgroundColor = UIColor(hexString: "#4EBFFF")
titleLbl.text = "Success"
titleLbl.textColor = UIColor(hexString: "#4EBFFF")
messageLbl.text = "Your ticket has been created."
tryAgainButton.isHidden = true
iconStatus.image = UIImage(named: "icon-check")
case .failure:
dashIMageView.image = UIImage(named: "circle-dash-red")
circleView.backgroundColor = UIColor(hexString: "#EB3708")
titleLbl.text = "Failure"
titleLbl.textColor = UIColor(hexString: "#EB3708")
messageLbl.text = "There was an error, creating your ticket."
tryAgainButton.isHidden = false
iconStatus.image = UIImage(named: "icon-close")
}
parentView.center = to.view.center
to.view.addSubview(parentView)
}
func dismissAlert() {
parentView.removeFromSuperview()
}
@IBAction func tryAgainButtonTapped(_ sender: AnimatableButton) {
delegate?.tryAgainTapped()
}
}
这就是我所谓的视图
class CreateTicketViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
AlertView.sharedInstance.delegate = self
}
@IBAction func createTicketTapped(_ sender: AnimatableButton) {
AlertView.sharedInstance.showAlert(alertType: .failure, to: self)
}
}
extension CreateTicketViewController : FailViewDelegate {
func tryAgainTapped() {
print("Try Again Tapped")
}
}
这是我得到的错误
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
(在dashIMageView.image = UIImage(名称:“ circle-dash-red”)中),当我删除dashImageView时,就会发生nextView等错误
答案 0 :(得分:1)
您不需要将其设置为单例,在这种情况下,(视图)是一种非常不常见的方法,我认为。您可以随时随地(在其他ViewController上)创建任意数量的该视图实例,并以所需的方式指定它们。
当要渲染视图并使其可见时,该视图必须始终是当前可见视图控制器主视图中视图层次结构的一部分。一个视图当时只能具有一个超级视图,因此,每当向另一个超级视图添加一个(单个)视图时,该视图就会从另一个超级视图中删除。如果要在多个视图控制器上使用相同的视图(没问题),那就不要让它成为单例。
第一件事->通过注释掉该行来删除单例设计:
class AlertView: UIView {
// make this line a comment or just remove it
// static let instance = AlertView()
weak var delegate : FailViewDelegate?
在不同的视图控制器中,您只需创建AlertView的实例并按如下所示正确设置委托即可:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myAlertView = AlertView()
myAlertView.delegate = self
// then you don't need this anymore
// AlertView.sharedInstance.delegate = self
}