Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
来源:互联网 发布:powershell 登录linux 编辑:程序博客网 时间:2024/05/16 20:31
利用了大约一个多小时来搞明白OC中Blocks反向传值和Swift中Closure反向传值的差别,下面直接贴上代码:
一、第一个界面
// Created by 秦志伟 on 14-6-13.import UIKitclass 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中的typedeftypealias sendValueClosure=(string:String)->Voidclass 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交流群:爱疯、爱Coding:209476515
3 0
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
- Swift 正向传值以及利用闭包(closure)实现反向传值(七)
- Swift利用闭包反向传值
- swift中利用闭包进行反向传值
- Swift闭包(Closure)
- swift两个视图控制器之间的传值
- swift closure 闭包
- swift 闭包(closure)
- swift基础利用闭包在两个视图界面传值
- 【iOS开发-26】利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- 【iOS开发-26】利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- Swift之delegate代理和Block闭包的反向传值
- Swift中的闭包(Closure)
- swift学习之闭包(closure)
- Swift 闭包(Closure)回调传值
- swift之闭包(closure)
- 字符数组 字符指针 字符串常量
- 《python源码剖析》笔记 python中的整数对象
- C#中foreach的使用
- CUDA入门知识
- 计算两个文件的相对路径(php)
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
- OCP 1Z0 052 73
- linux ping unknown host
- memcached学习(4). memcached的分布式算法
- MagicalRecord 2.1
- GitLab安装之后没有样式问题
- Android 4.0 API新特性之OVERVIEW
- UIView和CALayer的区别
- OCP 1Z0 052 74