swift中的闭包传值(block)
2017-03-29 15:41
225 查看
原文地址:http://www.myexception.cn/operating-system/1684429.html
http://www.cocoachina.com/bbs/read.php?tid=230724
原文如下:
Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
利用了大约一个多小时来搞明白OC中Blocks反向传值和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.
}
}
二、第二个界面
// 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.
}
}
http://www.cocoachina.com/bbs/read.php?tid=230724
原文如下:
Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
利用了大约一个多小时来搞明白OC中Blocks反向传值和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.
}
}
二、第二个界面
// 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.
}
}
相关文章推荐
- Swift: 比较Swift中闭包传值、OC中的Block传值
- swift中闭包 OC中Block 解决循环引用
- swift闭包的使用 -- 类似于OC中的Block
- Swift中闭包实现OC的block传值
- swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)
- Swift 闭包(block)传值
- Swift之delegate代理和Block闭包的反向传值
- swift中闭包的使用(类似与block的做法)
- Swift 基本知识点之九 —通知,代理,闭包(OC中Block)从前向后传值解析
- 关于OC中的Block,Swift中的闭包,C++11中的lambda表达式等匿名函数详解
- block,代理和通知的简单介绍--swift闭包
- Swift 闭包即OC中的Block
- swift中闭包的使用(类似与block的做法)
- OC的block和Swift的闭包写法
- Objective-C中的block与Swift中的尾随闭包使用教程
- [Swift]Swift中使用闭包实现block回调事件
- swift里面利用闭包达到和OC里面block的效果
- swift 学习笔记3 -- OC的block与swift的闭包
- iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包,swift 3.0
- Swift 闭包 (oc 的block 类似)