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