Swift Delegate使用

    xiaoxiao2021-03-25  121

    我们以UItableViewcell里按钮点点击事件作为例子

    在cell里声明一个delegate

     

    weak var delegate:MZAccumulateCellDelegate?

    需要实现的方法

     

     

    protocol MZAccumulateCellDelegate:NSObjectProtocol { func buyButtonDidClick(sender:UIButton,model:contentModel) -> Void }

    给按钮添加点击事件

     

     

    buyBtn?.addTarget(self, action: #selector(clickBuyBtn(_:)), for: .touchUpInside)

    传递delegate

     

     

    func clickBuyBtn(_ sender:UIButton) -> Void { delegate?.buyButtonDidClick(sender: sender, model: model) }

    在VC里准守代理

     

     

    MZAccumulateCellDelegate

     

     

    cell?.delegate = self

    实现方法

     

     

    func buyButtonDidClick(sender: UIButton, model: contentModel) { if !MZOAuthManager.sharedInstance.isAuthorized() { self.gotoLogin() return; } let buyVC = MZBuyGoldViewController.init() buyVC.productId = model.id buyVC.isRegular = true self.navigationController?.pushViewController(buyVC, animated: true) }

     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-10049.html

    最新回复(0)