博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SWIFT语言实现代理传值/闭包传值
阅读量:4607 次
发布时间:2019-06-09

本文共 5436 字,大约阅读时间需要 18 分钟。

1.需求:利用代理实现反向传值(例子采用点击第二个视图控制器中的按钮来改变第一个视图控制器中的Label的内容)

一、第一个界面

1 class ViewController: UIViewController, ChangeTestLabelDelegate { 2     var testLabel: UILabel? 3     override func viewDidLoad() { 4         super.viewDidLoad() 5         // Do any additional setup after loading the view, typically from a nib. 6         self.title = "swift代理传值"; 7         let rightButtonItem: UIBarButtonItem = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage"); 8         self.navigationItem.rightBarButtonItem = rightButtonItem; 9         10         testLabel = UILabel(frame: CGRectMake(0, 200, 320, 50));11         testLabel?.layer.borderColor = UIColor.redColor().CGColor;12         testLabel?.layer.borderWidth = 1;13         testLabel?.layer.cornerRadius = 5;14         testLabel?.text = "学雨燕";15         testLabel?.textAlignment = NSTextAlignment.Center;16         self.view.addSubview(testLabel!);17     }18 19     func nextPage() {20         let secondVC: SecondViewController = SecondViewController();21         secondVC.delegate = self;22         self.navigationController?.pushViewController(secondVC, animated: true);23     }24     25     func changeLabelText(controller: SecondViewController, string: String) {26         testLabel?.text = string;27         println("testLabel.text == \(string)");28     }29     override func didReceiveMemoryWarning() {30         super.didReceiveMemoryWarning()31         // Dispose of any resources that can be recreated.32     }33 34 35 }

二、第二个界面

1 import Foundation 2 import UIKit 3  4 //定义协议改变Label内容 5 protocol ChangeTestLabelDelegate: NSObjectProtocol { 6     //回调方法 7     func changeLabelText(controller: SecondViewController, string: String); 8 } 9 10 class SecondViewController: UIViewController {11     var temp = 0;12     var delegate: ChangeTestLabelDelegate?13     override func viewDidLoad(){14         super.viewDidLoad()15         self.title = "SecondViewController";16         self.view.backgroundColor = UIColor.yellowColor();17         let rect = CGRect(x:50,y:200,width:150,height:50);18         var myButton = UIButton(frame:rect);19         myButton.center = CGPointMake(160,200);20         myButton.setTitle("改变Label内容",forState:.Normal);21         myButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal);22         myButton.addTarget(self,action:"btnClicked",forControlEvents:.TouchUpInside);23         self.view.addSubview(myButton);24         25     }26     func btnClicked(){27         temp++;28         println("我被点击了\(temp)次.")29         if(delegate != nil) {30             delegate?.changeLabelText(self, string: "学雨燕" + "\(temp)");31         }    32     }33 }

 2.需求:利用闭包实现反向传值

1 import UIKit 2  3 class ViewController: UIViewController { 4     var testLabel: UILabel? 5     override func viewDidLoad() { 6         super.viewDidLoad() 7         // Do any additional setup after loading the view, typically from a nib. 8         self.title = "swift闭包传值"; 9         let rightButtonItem: UIBarButtonItem = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage");10         self.navigationItem.rightBarButtonItem = rightButtonItem;11         12         testLabel = UILabel(frame: CGRectMake(0, 200, 320, 50));13         testLabel?.layer.borderColor = UIColor.redColor().CGColor;14         testLabel?.layer.borderWidth = 1;15         testLabel?.layer.cornerRadius = 5;16         testLabel?.text = "swift闭包";17         testLabel?.textAlignment = NSTextAlignment.Center;18         self.view.addSubview(testLabel!);19     }20 21     func nextPage() {22         let secondVC: SecondViewController = SecondViewController();23         secondVC.initWithClosure(someFunctionThatTakesAClosure);24         self.navigationController?.pushViewController(secondVC, animated: true);25     }26     func someFunctionThatTakesAClosure(string:String) -> Void {27         testLabel!.text = string28     }29     override func didReceiveMemoryWarning() {30         super.didReceiveMemoryWarning()31         // Dispose of any resources that can be recreated.32     }33 34 35 }
1 import Foundation 2 import UIKit 3 //类似于OC中的typedef 4 typealias changeLabelTextClosure = (string:String) -> Void; 5  6 class SecondViewController: UIViewController { 7     var myClosure: changeLabelTextClosure?   //声明一个闭包 8     var temp = 0; 9     func initWithClosure(closure: changeLabelTextClosure?) {10         myClosure = closure; //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用11     }12     override func viewDidLoad(){13         super.viewDidLoad()14         self.title = "SecondViewController";15         self.view.backgroundColor = UIColor.yellowColor();16         let rect = CGRect(x:50,y:200,width:150,height:50);17         var myButton = UIButton(frame:rect);18         myButton.center = CGPointMake(160,200);19         myButton.setTitle("改变Label内容",forState:.Normal);20         myButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal);21         myButton.addTarget(self,action:"btnClicked",forControlEvents:.TouchUpInside);22         self.view.addSubview(myButton);23         24     }25     func btnClicked(){26         temp++;27         println("我被点击了\(temp)次.")28             if (myClosure != nil){29                 //闭包隐式调用someFunctionThatTakesAClosure函数:回调。30                 myClosure!(string: "闭包传值\(temp)");31             }32     }33 }

 

转载于:https://www.cnblogs.com/ShawnLi/p/4446059.html

你可能感兴趣的文章
Python 字典 copy()方法
查看>>
Minimum Path Sum
查看>>
Remove Duplicates from Sorted Array II
查看>>
常量指针和指针常量巧妙记忆方法[转]
查看>>
python-haproxy作业讲解视频总结
查看>>
mui搜索框 搜索点击事件
查看>>
select2 下拉搜索控件
查看>>
WebAPI常见的鉴权方法,及其适用范围
查看>>
08. 删除重复&海量数据
查看>>
重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++
查看>>
发布mvc遇到的HTTP错误 403.14-Forbidden解决办法
查看>>
记录一些好用的工具
查看>>
超链接样式设置(去下划线)(转)
查看>>
2016012003+陈琦+散列函数的应用及其安全性
查看>>
Android 状态栏通知Notification、NotificationManager详解
查看>>
UIApplicationDelegate协议
查看>>
Jmeter测试dubbo接口填坑
查看>>
[zz]GDB调试精粹及使用实例
查看>>
数据库的创建和删除
查看>>
最简单的三层实例【插入据
查看>>