由FirstViewController跳转到SecondViewController的过程中,伴随着值的正向传递,在SecondViewController的操作完成之后,返回到FirstViewController的过程中也伴随着值的反向传递。
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()
}
}
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()
}
}
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()
}
}