Swift代理的使用

Swift代理的使用

1、申明代理

@objc protocol TLSelectViewDelegate{

    

    func TLSelectViewDidSelected()  //必须方法

    @objc optional func TLSelectViewQuit() //可选方法

}

 

 

2、定义一个代理属性

weak var  delegate: TLSelectViewDelegate?

 

 

3、使用者,首先加上

class TaskListViewController: UIViewController,TLSelectViewDelegate {

 

4、设置代理和实现

let cell: TLTaskListCell = tableView.dequeueReusableCell(withIdentifier: "TLTaskListCell") as! TLTaskListCell

cell.selectionStyle = .none;

cell.delegate = self

 

5、在需要的地方直接调用

if let delegateOK = self.delegate{

   delegateOK.TLSelectViewDidSelected()

}

猜你喜欢

转载自www.cnblogs.com/jukaiit/p/9110375.html