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