UIViewController界面跳转时的值传递

    由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()
    }
    
}

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()
    }
    
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值