Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
一、第一个界面
// 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)来实现传值-->前后两个控制器的反向传值的更多相关文章
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
利用了大约一个多小时来搞明确OC中Blocks反向传值和Swift中Closure反向传值的区别,以下直接贴上代码: 一.第一个界面 // Created by 秦志伟 on 14-6-13. imp ...
- IOS 学习笔记 2015-04-15 控制器数据反向传值
// // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...
- [ios][swift]使用swift闭包进行viewcontroller反向传值
闭包参考:http://c.biancheng.net/cpp/html/2285.html 闭包详解 传值参考:http://www.tuicool.com/articles/vy2uUz Sw ...
- Swift: 比较Swift中闭包传值、OC中的Block传值
一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...
- 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个 值传递给B类中得某个值(所以需要在B类中先准备一个变量 ...
- Swift基础之闭包Closure学习
首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...
- Swift语言精要-闭包(Closure)
闭包(Closure)这个概念如果没学过Swift的人应该也不会陌生. 学过Javascript的朋友应该知道,在Javascript中我们经常会讨论闭包,很多前端工程师的面试题也会问到什么是闭包. ...
- Swift 闭包反向传值
Swift中闭包反向传值 1.第二控制器申明一个闭包类型 typealias BackBlock = (String) -> Void 2.第二控制器定义一个变量 var BackBlockCl ...
- Swift:闭包
一.闭包的介绍 闭包表达式(Closure Expressions) 尾随闭包(Trailing Closures) 值捕获(Capturing Values) 闭包是引用类型(Closures Ar ...
随机推荐
- RMSE、RMS、标准差
1.均方根误差,它是观测值与真值偏差的平方和观测次数n比值的平方根,在实际测量中,观测次数n总是有限的,真值只能用最可信赖(最佳)值来代替.方根误差对一组测量中的特大或特小误差反映非常敏感,所以,均方 ...
- 转: JavaScript函数式编程(二)
转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...
- Delphi实现全局鼠标钩子
其中涉及到的一些API,网上均能查到详细的解释,这里不再熬述.源码下载 因为是全局钩子,所以要用dll注入.用到的鼠标消息结构如下: PMouseHookStruct = ^TMouseHookStr ...
- Unity3d 打包时报错 CommandInvokationFailure: Unable to convert classes into dex format. See the Console for details.
今天打包带有Android插件的unity3d 项目是,报错CommandInvokationFailure: Unable to convert classes into dex format. S ...
- opennebula auth module ldap
1,安装net-ldap addon ruby library for openldap
- heap creation
There two methods to construct a heap from a unordered set of array. If a array has size n, it can b ...
- [mysqldumpslow 报错] Died at /usr/local/mysql/bin/mysqldumpslow line 161, <> chunk 236.
mysqldumpslow报错:Died at /usr/local/mysql/bin/mysqldumpslow line 161, <> chunk 236. 总结:是由于top数目 ...
- UITabBarController使用详解
UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod 程序等.UITabBarController通常作为整个程序的rootViewCo ...
- iOS修改截取图片不规范问题
+(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{ UIImage ...
- Java SE基础部分——常用类库之SimpleDateFormat(日期格式化)
取得当前日期,并按照不同日期格式化输入.代码如下: // 20160618 SimpleDateFomat类的使用 日期格式化 练习 package MyPackage; //自己定义的包 impor ...