单击XIB按钮时刷新当前View Controller数据请求

时间:2019-04-15 04:16:23

标签: ios swift xib

我想通过错误处理页面访问我的所有视图控制器,因此当错误获取数据错误时,它不仅显示对话框,而且显示包含错误消息和刷新按钮的XIB文件。像这样:

enter image description here

这是XIB类代码:

import UIKit

class ErrorMessage: UIView {
    @IBOutlet weak var imageViewError: UIImageView!
    @IBOutlet weak var labelError: UILabel!
    @IBOutlet weak var buttonTryAgain: UIButton!
    static var message: String?

    override func awakeFromNib() {
        labelError.text = ErrorMessage.message
    }

    @IBAction func didTapTryAgain(_ sender: Any) {
        Dialog.showProgressDialog(info: "")
    }
}

这是我的基本控制器代码,负责处理所有问题。

import Foundation

class BaseViewController: UIViewController {

    var uiView =  UIView();

    override func viewDidLoad() {

    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        ErrorMessage.message = message
        guard let viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as? ErrorMessage else { return}
        self.view.addSubview(viewErrorMessage)
    }

}

这是在扩展BaseViewController的另一个类中的调用方式,因此它可以全局显示错误问题,而无需再次声明该类:

func onFailedDeleteCart(errorMessage: String) {
        getErrorMessage(message: errorMessage)
    }

现在我可以传递错误消息了。

问题是,我希望单击“刷新”按钮时刷新当前的View Controller。也许当我单击它时调用当前View Controller的viewDidLoad将是不错的逻辑,但是我不知道如何在XIB类中实现它。任何人都可以解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

方法:1

步骤:1为回调创建封包

typealias RefreshBlock = (()->())?

步骤:2在您的UIView类中定义闭包

步骤:3如果用户点击刷新按钮,则关闭通话

class ErrorMessage: UIView {
    @IBOutlet weak var imageViewError: UIImageView!
    @IBOutlet weak var labelError: UILabel!
    @IBOutlet weak var buttonTryAgain: UIButton!
    var refreshBlock:RefreshBlock!
    static var message: String?

    override func awakeFromNib() {
        labelError.text = ErrorMessage.message
    }

    // Step : 3
    @IBAction func didTapTryAgain(_ sender: UIButton) {
        refreshBlock!()
    }
}

步骤:4当addSubview调用

时,在闭包中分配值
class BaseViewController: UIViewController {

    override func viewDidLoad() {
    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        ErrorMessage.message = message
        guard let viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as? ErrorMessage else { return}
        viewErrorMessage.refreshBlock = {()
            self.viewDidLoad()
            print("Refresh Contents")
        }   
        self.view.addSubview(viewErrorMessage)
    }

}

方法:2

将当前的UIViewController引用传递到UIView类中。请参考下面的代码。

class ErrorMessage: UIView {
    @IBOutlet weak var imageViewError: UIImageView!
    @IBOutlet weak var labelError: UILabel!
    @IBOutlet weak var buttonTryAgain: UIButton!
    var currentVC:UIViewController!
    static var message: String?

    override func awakeFromNib() {
        labelError.text = ErrorMessage.message
    }

    @IBAction func didTapTryAgain(_ sender: UIButton) {
        currentVC.viewDidLoad()
    }
}


class BaseViewController: UIViewController {

    override func viewDidLoad() {

    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        ErrorMessage.message = message
        guard let viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as? ErrorMessage else { return}
        viewErrorMessage.currentVC = self
        self.view.addSubview(viewErrorMessage)
    }

}