UIViewController界面跳转时的值传递
来源:互联网 发布:windows pe镜像下载 编辑:程序博客网 时间:2024/06/06 03:41
由FirstViewController跳转到SecondViewController的过程中,伴随着值的正向传递,在SecondViewController的操作完成之后,返回到FirstViewController的过程中也伴随着值的反向传递。
FirstViewController------>------SecondViewController的正向值传递:
FirstViewController.swift
import UIKitclass FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "FirstView" self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() var btn = UIButton(frame: CGRectMake(30, 100, 100, 40)) btn.backgroundColor = UIColor.greenColor() btn.setTitle("点击跳转", forState: UIControlState.Normal) btn.addTarget(self, action: "toSecondView", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(btn) } func toSecondView() -> Void { let secondViewContrller = SecondViewController() secondViewContrller.titleText = "From FirstViewController" secondViewContrller.titleColor = UIColor.redColor() self.navigationController?.pushViewController(secondViewContrller, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
SecondViewController.swift
import UIKitclass SecondViewController: UIViewController{ var titleText: String? var titleColor: UIColor? override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = titleText self.navigationController?.navigationBar.barTintColor =titleColor } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
FirstViewController------>------SecondViewController的反向值传递:
[说明]UIViewController跳转时值的反向传递有两种方式,代理和闭包。
-->-->
( - ) 代理的方法
SecondViewController.swift
import UIKit// 自定义的代理protocol SecondViewDelegate: NSObjectProtocol{ // 在代理中定义方法, 修改title的内容 func changeTitleContent(title: String) // 修改背景颜色 func changeBackgroundColor(color: UIColor)}class SecondViewController: UIViewController{ var delegate: SecondViewDelegate? override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "SecondView" var btn = UIButton(frame: CGRectMake(20, 90, 170, 40)) btn.backgroundColor = UIColor.redColor() btn.setTitle("返回", forState: UIControlState.Normal) btn.addTarget(self, action: "backToFirst", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(btn) } func backToFirst() -> Void { delegate?.changeTitleContent("Back From SecondViewController") delegate?.changeBackgroundColor(UIColor.purpleColor()) self.navigationController?.popViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
FirstViewController.swift
import UIKit// 在FirstViewController中实现SecondViewDelegateclass FirstViewController: UIViewController, SecondViewDelegate{ override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "FirstView" var btn = UIButton(frame: CGRectMake(30, 100, 100, 40)) btn.backgroundColor = UIColor.greenColor() btn.setTitle("点击跳转", forState: UIControlState.Normal) btn.addTarget(self, action: "toSecondView", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(btn) } func toSecondView() -> Void { let secondViewContrller = SecondViewController() secondViewContrller.delegate = self self.navigationController?.pushViewController(secondViewContrller, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func changeBackgroundColor(color: UIColor) { //重写SecondViewDelegate的方法 self.view.backgroundColor = color } func changeTitleContent(title: String) { //重写SecondViewDelegate的方法 self.title = title } }
( - ) 闭包的方式回传数据
SecondViewController.swift
import UIKitclass SecondViewController: UIViewController{ // 定义一个闭包 var changeContent: ((title: String, color: UIColor) -> Void)? override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "Second" var btn = UIButton(frame: CGRectMake(20, 90, 170, 40)) btn.backgroundColor = UIColor.redColor() btn.setTitle("返回", forState: UIControlState.Normal) btn.addTarget(self, action: "backToFirst", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(btn) } func backToFirst() -> Void { // 通过闭包回传数据 changeContent?(title: "From SecondViewController", color: UIColor.purpleColor()) self.navigationController?.popViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
FirstViewController.swift
import UIKitclass FirstViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "First" var btn = UIButton(frame: CGRectMake(30, 100, 100, 40)) btn.backgroundColor = UIColor.greenColor() btn.setTitle("点击跳转", forState: UIControlState.Normal) btn.addTarget(self, action: "toSecondView", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(btn) } func toSecondView() -> Void { let secondViewController = SecondViewController() // 设置值的接收方式 secondViewController.changeContent = { (title: String, color: UIColor) in self.title = title self.view.backgroundColor = color } self.navigationController?.pushViewController(secondViewController, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
1 0
- UIViewController界面跳转时的值传递
- 界面的跳转与值传递
- Swift-->UIViewController的跳转,参数传递,返回参数
- 跳转界面传递数据
- 【UIViewController界面传值】
- iOS 6.0中UIViewController被弃用的一些方法(界面跳转等)
- iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- iOS- 浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- UIViewController之间传递值的两种方式
- SharedPreferences---保存数据、在界面跳转时传递数值
- UIViewController页面跳转的两种方式
- CATransition 两个UIViewController 之间的跳转动画
- IOS 不同的 UIViewController 之间如何跳转
- UIViewController界面布局的一些坑
- UIViewController的生命周期及界面切换
- oracle的参数优化
- HMM:隐马尔科夫模型-前向算法
- 标准IO库 知识点百问《APUE》 chapter-5
- C#多线程学习(三) 生产者和消费者
- 如何生成KeyStore
- UIViewController界面跳转时的值传递
- 检测地图上的河流,基于openCV
- C#多线程学习(四) 多线程的自动管理(线程池)
- $str_2 = "str_1 : ${str_1}2 <br>"; //引用的变量名后,多了个字符2 即$str_12
- 第三届_求解算式
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- Cocos2d-x 3.x瓦片地图
- 更新ListView的几种方法
- AFN 设置请求数据类型