一、第一个界面

//  Created by 秦志伟 on 14-6-13.
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.
}
*/ }

二、第二个界面

//  Created by 秦志伟 on 14-6-13.
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.
}
*/ }

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

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

    利用了大约一个多小时来搞明确OC中Blocks反向传值和Swift中Closure反向传值的区别,以下直接贴上代码: 一.第一个界面 // Created by 秦志伟 on 14-6-13. imp ...

  2. IOS 学习笔记 2015-04-15 控制器数据反向传值

    // // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...

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

    闭包参考:http://c.biancheng.net/cpp/html/2285.html   闭包详解 传值参考:http://www.tuicool.com/articles/vy2uUz Sw ...

  4. Swift: 比较Swift中闭包传值、OC中的Block传值

    一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...

  5. 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值

    实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个 值传递给B类中得某个值(所以需要在B类中先准备一个变量 ...

  6. Swift基础之闭包Closure学习

    首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...

  7. Swift语言精要-闭包(Closure)

    闭包(Closure)这个概念如果没学过Swift的人应该也不会陌生. 学过Javascript的朋友应该知道,在Javascript中我们经常会讨论闭包,很多前端工程师的面试题也会问到什么是闭包. ...

  8. Swift 闭包反向传值

    Swift中闭包反向传值 1.第二控制器申明一个闭包类型 typealias BackBlock = (String) -> Void 2.第二控制器定义一个变量 var BackBlockCl ...

  9. Swift:闭包

    一.闭包的介绍 闭包表达式(Closure Expressions) 尾随闭包(Trailing Closures) 值捕获(Capturing Values) 闭包是引用类型(Closures Ar ...

随机推荐

  1. Oracle EBS-SQL (BOM-8):检查物料属性(无采购员).sql

    select       msi.segment1                                  物料编码,       msi.DESCRIPTION               ...

  2. Mac OS X:Dock 的附加功能

    Dock 提供了可能并非显而易见的有用控件和菜单.通过修饰键的不同组合(如 Option 键.Control 键)及鼠标点按的不同类型(点按与按下及按住),即可使用下列附加功能.本文适用于 Mac O ...

  3. java 书籍推荐 JavaEE程序员必读图书大推荐

    java 书籍推荐 JavaEE程序员必读图书大推荐 转自:http://www.cnblogs.com/xlwmin/articles/2192775.html 下面是我根据多年的阅读和实践经验,给 ...

  4. poj1338

                                                                                 Ugly Numbers Time Limit ...

  5. Mac/ios 模拟器 测试模拟慢网速

    原文:http://www.heyuan110.com/2015/06/16/Mac%E6%B5%8B%E8%AF%95%E6%A8%A1%E6%8B%9F%E6%85%A2%E7%BD%91%E9% ...

  6. sqlplus handbook

    1.直接敲sqlplus并回车就是启动SQL*PLUS,输入user及password将使用户登陆到缺省的数据库. 请输入用户名: 2.sqlplus user/password@SERVICE_NA ...

  7. 开源消息总线ActiveMQ

    一.消息中间件MOM(Message-Oriented Middleware) 消息中间件是解决异步分布式系统中通讯和排队问题的中间件技术.它利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数 ...

  8. 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit

    this bundle is invalid . new apps and app updates submitted to the app store must be built with publ ...

  9. mvp框架

    本文在于巩固基础 mvp框架的概念: MVP是MVC模式的另一个变种,MVP即可以应用到WEB项目中, 也可以应用到Winform项目中,它的方便的测试机制为大型复杂的企业级应用带来了福音,MVP模式 ...

  10. JFreeChat

    JFreeChart教程(一) 分类: java Component2007-05-31 15:53 39849人阅读 评论(30) 收藏 举报 jfreechartimportdataset图形ap ...