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 ...
随机推荐
- 简洁的jsp
在开发 是使用tomcat7版本(7的jslt表达式语法检查更加严格) 1.去除生产html的不必要的空行 <%@ page trimDirectiveWhitespaces="tru ...
- SQLServer,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表xx中的标识列指定显式值
情景: 如果此表的主键或者其中有一个列使用了 IDENTITY(1,1) 自增长时,但又想手动为此列指定值时,当用如下解决方案: set identity_insert 表名 ON 使用此命令把表的 ...
- wx.Dialog
wx.Dialog A dialog box is a window with a title bar and sometimes a system menu, which can be moved ...
- 可解压带中文名称文件的zip包
package com.text.ziptest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; i ...
- 设计模式六大原则——迪米特法则(LoD)
1.背景 在图书馆借书.刚開始的时候,直接跑到对应的楼层去,到里面去转,去找要借的书,在里面溜达半天才干找到:后来知道图书馆有一个电脑查询处.然后直接在电脑上输入想要借的书,电脑就会显示你想要借的书的 ...
- Android API Level在11前后及16之后时Notification的不同用法
作为刚入门Android的小白,最近在按照郭大神的<第一行代码>在练习,在用到Notification时遇到了一些问题,网上资料比较零散,我这里做了一个总结分析给各位,若有错误,恳请指正~ ...
- android入门——BroadCast(2)
自定义广播 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&q ...
- 详解VB.net编写DLL(动态链接库、应用程序扩展)文件
首先,我们启动VS(Visual-Studio简称),我使用的是VS2008版本. 新建一个项目-选择内裤(额...不好意思)→类库 ,名称就默认吧. 编写类库没有窗体设计,因此我们不能使用工具箱中的 ...
- Quartz 2D 概述
Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境.我们可以使用Quartz 2D API来实现许多功能,如基本路径的绘制.透明度.描影.绘制阴影.透明层.颜色管理.反锯齿 ...
- JAVA 年轻代收集器 第九节
JAVA 年轻代收集器 第九节 继续上一章所讲的,STW即GC时候的停顿时间,他会暂停我们程序中的所有线程.如果STW所用的时间长而且次数多的话,那么我们整个系统稳定性以及可用性将大大降低. 因此我 ...