zoukankan      html  css  js  c++  java
  • [ios][swift]使用swift闭包进行viewcontroller反向传值

    闭包参考:http://c.biancheng.net/cpp/html/2285.html   闭包详解

    传值参考:http://www.tuicool.com/articles/vy2uUz   

    Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值

    import UIKit
    
    class ZWRootViewController: UIViewController {
    
      init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
      }
      var myLabel:UILabel?
      override func viewDidLoad() {
        super.viewDidLoad()
        
        var item = UIBarButtonItem(title:"下一页",style:UIBarButtonItemStyle.Plain,target:self,action:"nextBtnClicked")
        self.navigationItem.rightBarButtonItem = item
        
        
        myLabel = UILabel(frame:CGRectMake(0,100,320,50))
        myLabel!.text = "Closure"
        myLabel!.textAlignment = NSTextAlignment.Center
        self.view.addSubview(myLabel!)
        // Do any additional setup after loading the view.
      }
      func someFunctionThatTakesAClosure(string:String) -> Void {
        // function body goes here
        myLabel!.text = string
      }
      func nextBtnClicked(){
        let second = ZWSecondViewController(nibName:nil,bundle:nil)
        //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数
        second.initWithClosure(someFunctionThatTakesAClosure)
        self.navigationController.pushViewController(second,animated:true)
        
      }
      
      override func viewWillDisappear(animated: Bool){
        myLabel!.hidden = true
      }
      override func viewWillAppear(animated: Bool){
        myLabel!.hidden = false
      }
      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
      }
      
    
      /*
      // #pragma mark - Navigation
    
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
      }
      */
    
    }
    import UIKit
    //类似于OC中的typedef
    typealias sendValueClosure=(string:String)->Void
    class ZWSecondViewController: UIViewController {
      var i:Int?
      //声明一个闭包
      var myClosure:sendValueClosure?
      //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针
      func initWithClosure(closure:sendValueClosure?){
        //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用
        myClosure = closure
      }
      
      init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        // Custom initialization
      }
    
      override func viewDidLoad() {
        super.viewDidLoad()
        i = 0
        var btn = UIButton.buttonWithType(UIButtonType.System) as?UIButton
        btn!.frame = CGRectMake(0,100,320,50)
        btn!.setTitle("点击我" ,forState:UIControlState.Normal)
        btn!.addTarget(self,action:"action", forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
        
        // Do any additional setup after loading the view.
      }
      func action(){
        i = i!+1
        //判空
        if myClosure{
          //闭包隐式调用someFunctionThatTakesAClosure函数:回调。
          myClosure!(string: "好好哦(i)")
        }
      }
      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
      }
      
    
      /*
      // #pragma mark - Navigation
    
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
      }
      */
    
    }
    

      

  • 相关阅读:
    [CSP-S模拟测试]:attack(支配树+LCA+bitset)
    [杂题]:C/c(二分答案)
    [杂题]:B/b(二分答案)
    二维莫队(离线)
    [CSP-S模拟测试]:联盟(搜索+树的直径)
    [CSP-S模拟测试]:蔬菜(二维莫队)
    [CSP-S模拟测试]:施工(DP+单调栈+前缀和)
    [CSP-S模拟测试]:画作(BFS+数学)
    [CSP-S模拟测试]:折射(DP)
    [CSP-S模拟测试]:养花(分块)
  • 原文地址:https://www.cnblogs.com/lyggqm/p/5098220.html
Copyright © 2011-2022 走看看