Swift Delegate使用

来源:互联网 发布:mac npm安装webpack 编辑:程序博客网 时间:2024/06/05 04:34

我们以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)    }


0 0
原创粉丝点击