swift delegate 和 block 使用

delegate使用

//自定義cell 代碼

import UIKit
//聲明代理協(xié)議
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("獲取驗(yàn)證碼")
        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調(diào)用

//遵守協(xié)議
class JRMainViewController: JRRootViewController,btnClickDelegate,UITableViewDelegate,UITableViewDataSource {}
//設(shè)置代理
 let loginCell = tableView.dequeueReusableCell(withIdentifier: "JRLoginTableViewCell", for: indexPath)as! JRLoginTableViewCell
loginCell.selectionStyle = .none
loginCell.delegate = self ;
return loginCell
//實(shí)現(xiàn)協(xié)議方法
  //MARK: - btnClickDelegate
    func btnclickMethod(tag: Int) {
        print(tag)
        if tag == 1 {
            //獲取驗(yàn)證碼
            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) ->()
    //創(chuàng)建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("獲取驗(yàn)證碼")
        //block調(diào)用
        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調(diào)用

let loginCell = tableView.dequeueReusableCell(withIdentifier: "JRLoginTableViewCell", for: indexPath)as! JRLoginTableViewCell
loginCell.selectionStyle = .none
//block 回調(diào)
loginCell.blockproerty = {(backMsg) in
               print(backMsg);
}
return loginCell
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS網(wǎng)絡(luò)架構(gòu)討論梳理整理中。。。 其實(shí)如果沒有APIManager這一層是沒法使用delegate的,畢竟多個(gè)單...
    yhtang閱讀 5,490評(píng)論 1 23
  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,622評(píng)論 30 472
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,338評(píng)論 4 61
  • block使用 第一部分 定義和使用Block, 定義在-viewDidLoad方法外部 定義Block變量,就相...
    levinYuXiao閱讀 7,980評(píng)論 2 22
  • 還有一場(chǎng)旅行要啟程 是去和遠(yuǎn)方的自己告別 稻草人流落在田間 而你是無畏的少年 剩下一首歌的時(shí)間 給青春一個(gè)吻別 然...
    decidels閱讀 116評(píng)論 0 0

友情鏈接更多精彩內(nèi)容