p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #535b60; background-color: #ffffff }
span.s1 { }
span.s2 { font: 14.0px Monaco; color: #c7254e; background-color: #f8f8f8 }

委托是一种设计模式,它允许类或结构体将一些需要它们负责的功能交由给其他的类型

委托模式的实现很简单: 定义协议来封装那些需要被委托的函数和方法,使其遵循着拥有这些被委托的函数和方法

//制定需要遵守的协议,制定协议遵守NSObjectProtocol协议

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #ffffff }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "PingFang SC"; color: #4dbf56 }
span.s1 { color: #c2349b }
span.s2 { }
span.s3 { color: #00afca }
span.s4 { font: 24.0px Menlo; color: #ffffff }
span.s5 { font: 24.0px Menlo }

protocol bottomViewDelegate : NSObjectProtocol {

//设置协议的方法

func bottomViewClick(btn : UIButton)

}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #ffffff }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #ffffff; min-height: 28.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #4dbf56 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #00afca }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "PingFang SC"; color: #4dbf56 }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #e44347 }
span.s1 { color: #c2349b }
span.s2 { }
span.s3 { color: #00afca }
span.s4 { color: #ffffff }
span.s5 { font: 24.0px "PingFang SC" }
span.s6 { color: #93c96a }
span.s7 { color: #e44347 }
span.s8 { font: 24.0px "PingFang SC"; color: #e44347 }
span.s9 { color: #8b84cf }
span.s10 { font: 24.0px Menlo; color: #ffffff }
span.s11 { font: 24.0px Menlo }

class BottomView: UIView {

var btn : UIButton!

//用weak定义代理

weak var delegate : bottomViewDelegate?

override init (frame : CGRect){

super.init(frame: frame)

let arr = ["日","月","周"]

for i  in 0..<3 {

btn = UIButton(type: .custom)

btn.frame = CGRect(x: 375/3*i , y: 0, width: 375/3, height: 44)

btn.setTitle(arr[i], for: .normal)

btn.tag = i

btn.backgroundColor = UIColor.gray

btn.setTitleColor(i == 1 ? UIColor.blue : UIColor.black, for: .normal)

btn.addTarget(self, action: #selector(clickedButton(button:)), for: .touchUpInside)

self.addSubview(btn)

}

}

func clickedButton(button : UIButton){

//判断代理是否存在,让代理去执行方法

delegate?.bottomViewClick(btn: button )

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "PingFang SC"; color: #4dbf56 }
span.s1 { }
span.s2 { font: 24.0px Menlo }

04-16 11:37