Swift常用的界面传值(属性传值、协议传值、闭包传值)

1、属性传值

属性传值多用于正向传值(A->B)

######A控制器

import UIKit
class ViewController: UIViewController {
    let  Screen = CGSizeMake(200, 40)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "第一个控制器"
        self.view.backgroundColor = UIColor.whiteColor()
        // 设置一个点击按钮,驱动事件跳转到下一个控制器
        let  Btn = UIButton(type: UIButtonType.Custom) as UIButton
        Btn.frame = CGRectMake(20, 100, Screen.width, Screen.height)
        Btn.setTitle("跳转到第二个控制器", forState: UIControlState.Normal)
        Btn.setTitleColor(UIColor.magentaColor(), forState: UIControlState.Normal)
        Btn.addTarget(self, action: "BtnClick", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(Btn)
    }
    //按钮点击后,事件的触发
    func BtnClick(){
        //现获取,跳转的控制器对象
        let TVC = TwoViewController()
        //传入一个字符串,给第二个控制器
        TVC.TSTR = "第二个控制器"
        //在传入一个颜色对象
        TVC.TColoer = UIColor.blueColor()
        //实现界面的跳转
        self.navigationController?.pushViewController(TVC, animated: true)
    }
}
######B控制器

import UIKit
class TwoViewController: UIViewController {
    //首先,我们要创建两个对象,来接受上一个界面传来的值
    var  TSTR :String? // 创建一个字符串对象
    var  TColoer :UIColor? //创建一个UIColoer 的颜色对象
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置控制器的标题
        self.title = self.TSTR
        // 设置背景颜色
        self.view.backgroundColor = self.TColoer
    }
}

2、代理传值

代理传值多用于反向传值(B->A)

######A控制器

import UIKit
class FViewController: UIViewController,FTVCdelegte{
    override func viewDidLoad() {
        super.viewDidLoad()
        //我们要创建一个返回按钮
        let  Fbtn = UIButton(type: UIButtonType.Custom) as UIButton
        Fbtn.frame = CGRectMake(30, 100, self.view.frame.size.width-60, 40)
        Fbtn.setTitle("跳转到第二个界面", forState: UIControlState.Normal)
        Fbtn.addTarget(self, action: "Click", forControlEvents: UIControlEvents.TouchUpInside)
        Fbtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        self.view.addSubview(Fbtn)
    }
    //跳转触发事件
    func Click(){
        let Tvc = FtwoViewController()
        Tvc.delegate_zsj = self
        self.navigationController?.pushViewController(Tvc, animated: true)
    }
    //实现协议的方法
    func change(title: String) {
        self.title = title
    }
}
######B控制器

import UIKit
//要创建一个协议
protocol FTVCdelegte : NSObjectProtocol{
    //在协议里面声明方法
    func change(title:String)
}
class FtwoViewController: UIViewController {
    //创建一个准守协议的对象
    var delegate_zsj :FTVCdelegte?
    override func viewDidLoad() {
        super.viewDidLoad()
        //我们要创建一个返回按钮
        let  Fbtn = UIButton(type: UIButtonType.Custom) as UIButton
        Fbtn.frame = CGRectMake(30, 100, self.view.frame.size.width-60, 40)
        Fbtn.setTitle("返回上一个界面", forState: UIControlState.Normal)
        Fbtn.addTarget(self, action: "Click", forControlEvents: UIControlEvents.TouchUpInside)
        Fbtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        self.view.addSubview(Fbtn)
    }
    //返回,事件的触发
    func Click(){
        delegate_zsj?.change("首页")
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}

3、闭包传值

闭包传值多用于反向传值(B->A)

######A控制器

import UIKit
class BBViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
       //创建一个按钮
        let  btn = UIButton(type: UIButtonType.Custom) as UIButton
        btn.frame = CGRectMake(30, 100, self.view.frame.size.width-60, 40)
        btn.setTitle("跳转到第二个控制器", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.magentaColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "Click", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
    }
    func Click(){
        let  BBTC = BBTViewController()
        BBTC.bbchange = { (title:String,coloer:UIColor) in
            self.title = title
            self.view.backgroundColor = coloer
        }
        self.navigationController?.pushViewController(BBTC, animated: true)
    }
}
######B控制器

import UIKit
class BBTViewController: UIViewController {
    //定义一个闭包,带有两个参数
    var bbchange :((title:String,coloer:UIColor)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.greenColor()
        //创建一个按钮
        let  btn = UIButton(type: UIButtonType.Custom) as UIButton
        btn.frame = CGRectMake(30, 100, self.view.frame.size.width-60, 40)
        btn.setTitle("返回到第1个控制器", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.magentaColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "bClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
    }
    func bClick(){
        bbchange?(title:"成龙",coloer:UIColor.redColor())
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}

你可能感兴趣的:(Swift常用的界面传值(属性传值、协议传值、闭包传值))