Swift3.0-反向传值
来源:互联网 发布:数据采集与处理小木虫 编辑:程序博客网 时间:2024/06/08 18:42
Swift3.0-反向传值
1、使用代理协议
在ViewController.swift中
class ViewController: UIViewController,SubDelegate { override func viewDidLoad() { super.viewDidLoad()// setNavigation() myButton() } func myButton() { let btn = UIButton(type:UIButtonType.system) as UIButton btn.frame = CGRect(x:60,y:100,width:100,height:30) btn.backgroundColor = UIColor.lightGray btn.setTitle("进入下一界面", for: UIControlState.normal) self.view.addSubview(btn) btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside) } func click(){ let vc = SubViewController() vc.delegate = self// let vc = SubClosureViewController()// vc.changeTitleAndClosure = {// (title:String,color:UIColor) in// self.title = title// self.view.backgroundColor = color// } self.navigationController?.pushViewController(vc, animated: true) } func changeTitle(title: String) { self.title = title } func changeColor(color: UIColor) { self.view.backgroundColor = color } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}在SubViewController.swift中
//在委托中定义协议protocol SubDelegate:NSObjectProtocol { //实现一些方法 func changeTitle(title:String) func changeColor(color:UIColor)}class SubViewController: UIViewController { //定义遵守协议 var delegate:SubDelegate? override func viewDidLoad() { super.viewDidLoad() myView() } func myView() { self.view.backgroundColor = UIColor.red let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30)) tf.borderStyle = UITextBorderStyle.roundedRect tf.tag = 100 self.view.addSubview(tf) let btn = UIButton(type:UIButtonType.system) as UIButton btn.frame = CGRect(x:60,y:100,width:100,height:30) btn.backgroundColor = UIColor.lightGray btn.setTitle("返回上一界面", for: UIControlState.normal) self.view.addSubview(btn) btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside) } func click(){ let tf = self.view.viewWithTag(100) as? UITextField delegate?.changeTitle(title: tf!.text!) delegate?.changeColor(color: UIColor.blue) self.navigationController?.popViewController(animated: true) }}
2、使用闭包
在ViewController.swift中
class ViewController: UIViewController,SubDelegate { override func viewDidLoad() { super.viewDidLoad()// setNavigation() myButton() } func myButton() { let btn = UIButton(type:UIButtonType.system) as UIButton btn.frame = CGRect(x:60,y:100,width:100,height:30) btn.backgroundColor = UIColor.lightGray btn.setTitle("进入下一界面", for: UIControlState.normal) self.view.addSubview(btn) btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside) } func click(){// let vc = SubViewController()// vc.delegate = self let vc = SubClosureViewController() vc.changeTitleAndClosure = { (title:String,color:UIColor) in self.title = title self.view.backgroundColor = color } self.navigationController?.pushViewController(vc, animated: true) }}在SubClosureViewController.swift中
class SubClosureViewController: UIViewController { //定义一个闭包 var changeTitleAndClosure:((_ title:String,_ color:UIColor) -> Void)? override func viewDidLoad() { super.viewDidLoad() myView() } func myView() { self.view.backgroundColor = UIColor.red let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30)) tf.borderStyle = UITextBorderStyle.roundedRect tf.tag = 100 self.view.addSubview(tf) let btn = UIButton(type:UIButtonType.system) as UIButton btn.frame = CGRect(x:60,y:100,width:100,height:30) btn.backgroundColor = UIColor.lightGray btn.setTitle("返回上一界面", for: UIControlState.normal) self.view.addSubview(btn) btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside) } func click(){ let tf = self.view.viewWithTag(100) as? UITextField changeTitleAndClosure?(tf!.text!,UIColor.green) self.navigationController?.popViewController(animated: true) }}
2 0
- Swift3.0-反向传值
- swift3.0三种反向传值
- Swift3.0--GCD
- Swift3.0-基础知识点
- Swift3.0 代理传值,属性传值
- Swift3.0
- swift3.0
- Swift3.0
- swift3.0遍历数组拿到下标和元素以及反向遍历
- Swift3.0-字符串、数组、字典操作
- Swift3.0-函数和控制语句
- Swift3.0-类(class)
- Swift3.0-继承、构造、类扩展
- Swift3.0- 协议(protocol)
- Swift3.0-UITextField创建以及使用
- Swift3.0-视图控制器的生命周期
- Swift3.0-UserDefaults保存数据
- cocos2dx反向传值
- golang技术,理解goroutine
- 如何学习深度学习
- 合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数
- python解析数据包
- 进程的组织
- Swift3.0-反向传值
- 起风了
- NumPy学习笔记
- 数据库巡检
- gulp源码解析(二)—— vinyl-fs
- RecycleView的上拉加载、下拉刷新、EmptyView、setOnItemClickListener的封装
- java反编译通过.class获取.java文件
- Nginx + Tomcat HTTPS配置
- android 开发零起步学习笔记(二十七):android edittext不自动获取焦点