[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(,,,))
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.
}
*/ }
[ios][swift]使用swift闭包进行viewcontroller反向传值的更多相关文章
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
利用了大约一个多小时来搞明确OC中Blocks反向传值和Swift中Closure反向传值的区别,以下直接贴上代码: 一.第一个界面 // Created by 秦志伟 on 14-6-13. imp ...
- Swift 闭包反向传值
Swift中闭包反向传值 1.第二控制器申明一个闭包类型 typealias BackBlock = (String) -> Void 2.第二控制器定义一个变量 var BackBlockCl ...
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- IOS开发之SWIFT进阶部分
概述 上一篇文章<iOS开发系列--Swift语言> 中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用 ...
- Swift: 比较Swift中闭包传值、OC中的Block传值
一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...
- Swift基础之闭包Closure学习
首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...
- 从UWP到SWIFT-页面间反向传值
页面1跳转到页面2,在页面2点击button后,页面1的内容被改变.实际使用 protocol(就是c#中的interface),将页面1的viewcontroller转换为protocol传入页面2 ...
- 【原】iOS学习之Swift之语法2(精简版)
1.可选类型和强制解包(?和!) 1> 可选类型(?)和强制解包(!) 在swift中,可选类型(?) 其根源是一个 枚举型,里面有 None 和 Some 两种类型.其实所谓的 nil 就是 ...
- Swift基础之闭包
内容纲要: 1.闭包基础 2.关于闭包循环引用 正文: 1.闭包 闭包是自包含的函数代码块,可以在代码中被传递和使用.Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks) ...
随机推荐
- python gzip,bz2学习
一.gzip import gzip 1.解压缩 a = gzip.open('a.tar.gz') b = open('a.tar','wb') b.write(a.read()) a.close( ...
- JAX-WS(二)之使用wsimport创建WebService客户端
客户端开发的通常过程是从已有的WSDL处罚,创建辅助类JAXB对象和Service代理类,然后基于这些类开发自己的客户端应用. 开发步骤: 创建eclipse项目: 运行wsimport命令生成客户端 ...
- F面经:painting house
There are a row of houses, each house can be painted with three colors red, blue and green. The cost ...
- [原创]java WEB学习笔记76:Hibernate学习之路---Hibernate介绍,hibernate 环境的搭建
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [转]WEB开发者必备的7个JavaScript函数
我记得数年前,只要我们编写JavaScript,都必须用到几个常用的函数,比如,addEventListener 和 attachEvent,并不是为了很超前的技术和功能,只是一些基本的任务,原因是各 ...
- .NET: XML
XML在平常生活中用得很多,它的结构很简单,跟windows explorer有点像. 对它进行操作主要有三种方式:XmlDocument, 假设有这么一个XML文件Book.XML <?xml ...
- Android中操作数据的集中方式---文件,SQLite,ContentProvider
http://blog.csdn.net/he90227/article/details/33734239 转
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
开发者经验谈:如何一天时间搞定iOS游戏开发? 在一天时间里将完成iPhone游戏开发由梦想变为现实? 本文作者给出了从创意转变成现实的详细答案.使用苹果原生游戏引擎SpriteKit,遵循一定的原则 ...
- ansible自动化运维工具的安装与使用
运行环境 centOS6.6 ansible ansible的功能还是比较多的,博主只用它在集群上进行批量部署软件和维护的功能,其他不多做研究,有需要的话这篇文章会慢慢补充. ansible特点 轻量 ...
- sql except 用法,找两个表中非共同拥有的
;with tt as (select a.id as id from [dbo].[1234] a where a.id not in (select a.ProtocolID from Proto ...