4.4、协议代理与闭包传值

1、OneViewController

class OneViewController: UIViewController {
    var delega:SayHelloDelegate?
    var bibaolabel = UILabel()
    var texts = "我是一个标签"
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "一"
        self.setBaseset()
        let label = UILabel(frame: CGRect(x: 10, y: 100, width: mainWidth-20, height: 150))
        label.backgroundColor = UIColor.lightGray
        self.view.addSubview(label)
        label.textAlignment = .center
        label.text = self.texts
        label.numberOfLines = 0
        
        let label2 = UILabel(frame: CGRect(x: 10, y: 300, width: mainWidth-20, height: 150))
        label2.backgroundColor = UIColor.lightGray
        self.view.addSubview(label2)
        label2.numberOfLines = 0
        label2.textAlignment = .center
        self.bibaolabel = label2
        
        let btn = UIButton(frame: CGRect(x: 10, y: 550, width: mainWidth-20, height: 40))
        btn.backgroundColor = UIColor.green
        self.view.addSubview(btn)
        btn.addTarget(self, action: #selector(comeToNextVC), for: .touchUpInside)
    }
    //协议传值
    var name = "第一个页面的名字:Tina"
    func plays() {
        delega?.sayHello(name: name)
    }
    //闭包接收值
    @objc func comeToNextVC() {
        let two = TwoViewController()
        two.deliverDataBlock = {
            (a,b) in
            self.bibaolabel.text = "\(a)" + "\(b)"
        }
        self.navigationController?.pushViewController(two, animated: true)
    }

2、TwoViewController

class TwoViewController: UIViewController,SayHelloDelegate {
    var quanjuName = "初始值"
    var quanlab = UILabel()
    var bibaolabel = UILabel()
    //协议接收值
    func sayHello(name: String) {
        print("传过来的内容是:",name)
        self.quanjuName = name
        self.quanlab.text = self.quanjuName
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "二"
        self.setBaseset()
        let label = UILabel(frame: CGRect(x: 10, y: 100, width: mainWidth-20, height: 150))
        label.backgroundColor = UIColor.lightGray
        self.view.addSubview(label)
        label.numberOfLines = 0
        self.quanlab = label
        label.textAlignment = .center
        
        let label2 = UILabel(frame: CGRect(x: 10, y: 300, width: mainWidth-20, height: 150))
        label2.backgroundColor = UIColor.lightGray
        self.view.addSubview(label2)
        label2.numberOfLines = 0
        self.bibaolabel = label2
        
        let btn = UIButton(frame: CGRect(x: 10, y: 550, width: mainWidth-20, height: 40))
        btn.backgroundColor = UIColor.green
        self.view.addSubview(btn)
        btn.setTitle("当前页面委托代理", for: .normal)
        btn.addTarget(self, action: #selector(getLabelsData), for: .touchUpInside)

        let btn2 = UIButton(frame: CGRect(x: 10, y: 600, width: mainWidth-20, height: 40))
        btn2.backgroundColor = UIColor.red
        self.view.addSubview(btn2)
        btn2.addTarget(self, action: #selector(comeToNextVCs), for: .touchUpInside)
        btn2.setTitle("跳到其他页面委托代理", for: .normal)//学生自己做

        let btn23 = UIButton(frame: CGRect(x: 10, y: 650, width: mainWidth-20, height: 40))
        btn23.backgroundColor = UIColor.black
        self.view.addSubview(btn23)
        btn23.addTarget(self, action: #selector(comeBackLaseVC), for: .touchUpInside)
        btn23.setTitle("闭包传值", for: .normal)
    }
    @objc func getLabelsData() {
        let one = OneViewController()
        one.delega = self
        one.plays()
    }
    @objc func comeToNextVCs() {
        let one = ThreeViewController()
        self.navigationController?.pushViewController(one, animated: true)
    }
//闭包传值
    typealias typeBlock = (Int,Int)->()
    var deliverDataBlock:typeBlock?
    @objc func comeBackLaseVC() {
        if self.deliverDataBlock != nil {
            self.deliverDataBlock!(1,2)
        }
        self.comeNavgationControllerBack()
    }

3、ThreeViewController

class ThreeViewController: UIViewController,SayHelloDelegate {
    var quanlab = UILabel()
    func sayHello(name: String) {
        let str = name as NSString
        self.quanlab.text = "My name is" + str.substring(from: 9)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "三"
        self.setBaseset()
        let label = UILabel(frame: CGRect(x: 10, y: 100, width: mainWidth-20, height: 400))
        label.backgroundColor = UIColor.lightGray
        label.textAlignment = .center
        self.view.addSubview(label)
        label.numberOfLines = 0
        self.quanlab = label
        
        let btn = UIButton(frame: CGRect(x: 10, y: 550, width: mainWidth-20, height: 40))
        btn.backgroundColor = UIColor.green
        self.view.addSubview(btn)
        btn.setTitle("第三个页面获取协议数据", for: .normal)
        btn.addTarget(self, action: #selector(getThreeLabelsData), for: .touchUpInside)

        // Do any additional setup after loading the view.
    }
    //协议传值
    @objc func getThreeLabelsData() {
        let one = OneViewController()
        one.delega = self
        one.plays()
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容