第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

iOS學習筆記--Swift之閉包函數(shù)的傳值

標簽:
iOS

Swift闭包函数的传值 类比OC的Block 其实是一样的

简单介绍一下代码:FirstViewController 点击按钮 push到SecondViewController,当点击Block按钮的时候回调并pop到上一页,回调的参数显示在FirstViewController的按钮上。

FirstViewController代码:


import UIKit

class FirstViewController: UIViewController {

    var btn : UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.cyan

        btn = UIButton(type:UIButtonType.custom)
        btn!.frame = CGRect(x:40,y:80,width:120,height:35)
        btn!.layer.cornerRadius = 4
        btn!.layer.masksToBounds = true
        btn!.backgroundColor = UIColor.lightGray
        btn!.setTitle("按钮", for: UIControlState.normal)
        btn!.addTarget(self, action: #selector(FirstViewController.btnClicked), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn!)
    }

    func btnClicked() -> Void {

        let second = SecondViewController()
        second.callBackClosure = {
            (str , name) in
            self.btn!.setTitle(str, for: .normal)
        }
        self.navigationController?.pushViewController(second, animated: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

SecondViewController代码:


import UIKit

typealias SwiftClosure = (String,Int) -> Void

class SecondViewController: UIViewController {

    var callBackClosure:SwiftClosure?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.orange

        let btn = UIButton(type:UIButtonType.custom)
        btn.frame = CGRect(x:40,y:80,width:120,height:35)
        btn.backgroundColor = UIColor.red.withAlphaComponent(0.4)
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        btn.setTitle("Block按钮", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(SecondViewController.btnBlock), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)

    }

    func btnBlock() -> Void {
        if callBackClosure != nil{
            callBackClosure!("你的名字",26)
            self.navigationController!.popViewController(animated: true)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
點擊查看更多內(nèi)容
2人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消