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