zoukankan      html  css  js  c++  java
  • Swift-代理

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

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

     

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

    protocol bottomViewDelegate : NSObjectProtocol {

        //设置协议的方法

        func bottomViewClick(btn : UIButton)

    }

    class BottomView: UIView {

        

        var btn : UIButton!

        //weak定义代理

        weak var delegate : bottomViewDelegate?

        override init (frame : CGRect){

            super.init(frame: frame)

            let arr = ["","",""]

            forin 0..<3 {

                btn = UIButton(type: .custom)

                btn.frame = CGRect(x: 375/3*i , y: 0, 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")

        }

     

    }

     

  • 相关阅读:
    iOS objc_msgSend 报错解决方案
    不再以讹传讹,GET和POST的真正区别
    HTTP Get请求URL最大长度
    [转]浅论ViewController的加载 -- 解决 viewDidLoad 被提前加载的问题(pushViewController 前执行)
    ASIHTTPRequest-断点续传需要原网站支持!
    iOS关于error can't allocate region的一点发现
    Xcode 5.1.1 与 Xcode 6.0.1 的共存之路(建议大家在升级Xcode 6.0.1 的时候保留Xcode 5.1.1)
    监测uitableview 向上滑动和向下滑动的事件
    Xcode5和6共存时,如何发布应用到商店
    利用MPMoviePlayerViewController 播放视频 iOS
  • 原文地址:https://www.cnblogs.com/lcl15/p/6230564.html
Copyright © 2011-2022 走看看