UIViewController界面跳转时的值传递
2016-02-23 14:52
507 查看
由FirstViewController跳转到SecondViewController的过程中,伴随着值的正向传递,在SecondViewController的操作完成之后,返回到FirstViewController的过程中也伴随着值的反向传递。
FirstViewController------>------SecondViewController的正向值传递:
FirstViewController.swift
SecondViewController.swift
FirstViewController------>------SecondViewController的反向值传递:
[说明]UIViewController跳转时值的反向传递有两种方式,代理和闭包。
-->
-->
( - ) 代理的方法
SecondViewController.swift
FirstViewController.swift
( - ) 闭包的方式回传数据
SecondViewController.swift
FirstViewController.swift
FirstViewController------>------SecondViewController的正向值传递:
FirstViewController.swift
import UIKit class 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 UIKit class 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中实现SecondViewDelegate class 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 UIKit class 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 UIKit class 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() } }
相关文章推荐
- UIView
- easyUI 中datagrid控件从后台获取数据生成列,想让其中部分属性数据不在表中显示
- 我的Android进阶之旅------>解决:Failed to create 'build\outputs\apk\watch-debug-unaligned.apks': 拒绝访问。
- VS2008LINK : fatal error LNK1000: Internal error during IncrBuildImage
- bluetooth-蓝牙事件监听
- CoreBluetooth第二节:Performing Common Peripheral Role Task(执行常见的外设端任务)
- iOS开发技巧(系列十二:UUID和UDID的区别)
- mui如何增加自定义字体icon图标
- 详解 CALayer 和 UIView 的区别和联系
- iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewController
- easyUI 中datagrid控件demo,包括选中一行能读取到数据
- UIView和CALayer的区别 说的比较清楚了
- iOS-UItableview 分割线(自定义+原生)方法总结
- UI整理-----part7--模态视图 push&pop
- UI整理-----part6--页面间的传值
- Web UI 自动化测试环境搭建 (转载自51测试天地第三十九期上)
- 在keil mdk中的error: #28: expression must have a constant value
- 在非UI线程处理Bitmap
- 最简单的java GUI
- iOS开发第三方存储库YTKKeyValueStore