zoukankan      html  css  js  c++  java
  • swfit-block反向传值

    //  ViewController.swift
    //  Block
    
    import UIKit
    
    class ViewController: UIViewController {
    
        
        var myLabel = UILabel()
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.backgroundColor = UIColor.whiteColor()
            let btn = UIButton()
            btn.frame = CGRectMake(100, 300, 320, 50)
            btn.backgroundColor = UIColor.cyanColor()
            btn.setTitle("下一页", forState: UIControlState.Normal)
            btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            btn.addTarget(self, action: #selector(ViewController.nextBtnClicked), forControlEvents: UIControlEvents.TouchUpInside)
            self.view.addSubview(btn)
            myLabel.frame = CGRectMake(0, 100, 320, 50)
            myLabel.text = "Closure"
            myLabel.textAlignment = NSTextAlignment.Center
            self.view.addSubview(myLabel)
        }
        
        func someFunctionThatTakesAClosure(string:String) -> Void {
            
            myLabel.text = string
        }
        
        func nextBtnClicked(){
            
            let second = SecondViewController()
            //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数
            second.initWithClosure(someFunctionThatTakesAClosure)
            second.BtnName = "点我点我点我"
            self.presentViewController(second, animated: true) {
            }
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    //  SecondViewController.swift
    //  Block
    import UIKit
    typealias sendValueClosure=(string:String)->Void
    
    class SecondViewController: UIViewController {
    
    
        var BtnName : String = String()
        var i:Int?
        //声明一个闭包
        var myClosure:sendValueClosure?
        //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针
        func initWithClosure(closure:sendValueClosure?){
            //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用
            myClosure = closure
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            i = 0
            let btn = UIButton()
            btn.backgroundColor = UIColor.cyanColor()
            btn.frame = CGRectMake(0,100,320,50)
            let string : String = "点击我"
            if BtnName.characters.count == 0 {
                BtnName = string
            }
            btn.setTitle(BtnName ,forState:UIControlState.Normal)
            btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            btn.addTarget(self,action:#selector(SecondViewController.action), forControlEvents:UIControlEvents.TouchUpInside)
            self.view.addSubview(btn)
        
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func action(){
            i = i!+1
            //判空
            if (myClosure != nil){
                //闭包隐式调用someFunctionThatTakesAClosure函数:回调。
                myClosure!(string: "好好哦(i)")
            }
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    
    }
  • 相关阅读:
    手机获取ip地址
    CoreGraphics 自定义button
    抽奖及背景图片的透明度设置时连着转盘图片也跟着虚幻解决方法
    多个UIcollctionView,返回个数不对错误
    collectionview item 间距
    判断键盘的高度
    orcle 11g 的安装图解
    clone()详解
    isAssignableFrom ,isInstance , Instanceof() 区别
    三元表达式
  • 原文地址:https://www.cnblogs.com/sayimba/p/5779983.html
Copyright © 2011-2022 走看看