• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[ios][swift]使用swift闭包进行viewcontroller反向传值

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

 

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.
  }
  */

}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift、Flutter 共同瞄准 Windows!发布时间:2022-07-13
下一篇:
swift之SnapKit动画发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap