swift delegate 和 block 使用

delegate使用

//自定义cell 代码

import UIKit
//声明代理协议
protocol btnClickDelegate {
    //代理方法
    func btnclickMethod(tag:Int)
}
class JRLoginTableViewCell: UITableViewCell {
    //代理属性
    public var delegate:btnClickDelegate?
    @IBOutlet weak var getCodeBtn: UIButton!
    @IBOutlet weak var codeTF: UITextField!
    @IBOutlet weak var phoneTF: UITextField!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
 
    @IBAction func getCodeClick(_ sender: Any) {
//        print("获取验证码")
        if (delegate != nil) {
            delegate?.btnclickMethod(tag: 1)
        }
    }
    @IBAction func commitClick(_ sender: UIButton) {
//        print("马上开启")
        if (delegate != nil) {
            delegate?.btnclickMethod(tag: 2)
        }
    }
    @IBAction func textEidDidEnd(_ sender: UITextField) {
        print(sender.text as Any)
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

viewController调用

//遵守协议
class JRMainViewController: JRRootViewController,btnClickDelegate,UITableViewDelegate,UITableViewDataSource {}
//设置代理
 let loginCell = tableView.dequeueReusableCell(withIdentifier: "JRLoginTableViewCell", for: indexPath)as! JRLoginTableViewCell
loginCell.selectionStyle = .none
loginCell.delegate = self ;
return loginCell
//实现协议方法
  //MARK: - btnClickDelegate
    func btnclickMethod(tag: Int) {
        print(tag)
        if tag == 1 {
            //获取验证码
            getPhoneCode(phoneStr: "123213")
        }else if tag == 2{
            //登录
        }
    }

block使用
//自定义cell 代码

import UIKit

class JRLoginTableViewCell: UITableViewCell {
    //代理属性
    @IBOutlet weak var getCodeBtn: UIButton!
    @IBOutlet weak var codeTF: UITextField!
    @IBOutlet weak var phoneTF: UITextField!
    //定义block
    typealias fucBlock = (String) ->()
    //创建block变量
    var blockproerty:fucBlock!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
    }

    @IBAction func textEidDidEnd(_ sender: UITextField) {
        print(sender.text as Any)
    }
    
    @IBAction func getCodeClick(_ sender: Any) {
//        print("获取验证码")
        //block调用
        if let _ = blockproerty{
            blockproerty("1212")
        }
    }
    @IBAction func commitClick(_ sender: UIButton) {
//        print("马上开启")
        
        if let _ = blockproerty{
            blockproerty("334344")
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

viewController调用

let loginCell = tableView.dequeueReusableCell(withIdentifier: "JRLoginTableViewCell", for: indexPath)as! JRLoginTableViewCell
loginCell.selectionStyle = .none
//block 回调
loginCell.blockproerty = {(backMsg) in
               print(backMsg);
}
return loginCell
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,282评论 1 23
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,219评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • block使用 第一部分 定义和使用Block, 定义在-viewDidLoad方法外部 定义Block变量,就相...
    levinYuXiao阅读 7,863评论 2 22
  • 还有一场旅行要启程 是去和远方的自己告别 稻草人流落在田间 而你是无畏的少年 剩下一首歌的时间 给青春一个吻别 然...
    decidels阅读 111评论 0 0