Swift3.0-反向传值

Swift3.0-反向传值

1、使用代理协议

在ViewController.swift中

class ViewController: UIViewController,SubDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
//        setNavigation()
        myButton()
    }
    
    
    func myButton() {
        let btn = UIButton(type:UIButtonType.system) as UIButton
        btn.frame = CGRect(x:60,y:100,width:100,height:30)
        btn.backgroundColor = UIColor.lightGray
        btn.setTitle("进入下一界面", for: UIControlState.normal)
        self.view.addSubview(btn)
        
        btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
    }
    func click(){
        let vc = SubViewController()
        vc.delegate = self
//        let vc = SubClosureViewController()
//        vc.changeTitleAndClosure = {
//            (title:String,color:UIColor) in
//            self.title = title
//            self.view.backgroundColor = color
//        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func changeTitle(title: String) {
        self.title = title
    }
    
    func changeColor(color: UIColor) {
        self.view.backgroundColor = color
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
SubViewController.swift中
//在委托中定义协议
protocol SubDelegate:NSObjectProtocol {
    //实现一些方法
    func changeTitle(title:String)
    func changeColor(color:UIColor)
}

class SubViewController: UIViewController {

    //定义遵守协议
    var delegate:SubDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myView()
    }
    func myView() {
        self.view.backgroundColor = UIColor.red
        
        let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30))
        tf.borderStyle = UITextBorderStyle.roundedRect
        tf.tag = 100
        self.view.addSubview(tf)
        
        let btn = UIButton(type:UIButtonType.system) as UIButton
        btn.frame = CGRect(x:60,y:100,width:100,height:30)
        btn.backgroundColor = UIColor.lightGray
        btn.setTitle("返回上一界面", for: UIControlState.normal)
        self.view.addSubview(btn)
        
        btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
    }
    func click(){
        let tf = self.view.viewWithTag(100) as? UITextField
        delegate?.changeTitle(title: tf!.text!)
        delegate?.changeColor(color: UIColor.blue)
        self.navigationController?.popViewController(animated: true)
    }
}


2、使用闭包

在ViewController.swift中

class ViewController: UIViewController,SubDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
//        setNavigation()
        myButton()
    }
    
    
    func myButton() {
        let btn = UIButton(type:UIButtonType.system) as UIButton
        btn.frame = CGRect(x:60,y:100,width:100,height:30)
        btn.backgroundColor = UIColor.lightGray
        btn.setTitle("进入下一界面", for: UIControlState.normal)
        self.view.addSubview(btn)
        
        btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
    }
    func click(){
//        let vc = SubViewController()
//        vc.delegate = self
        let vc = SubClosureViewController()
        vc.changeTitleAndClosure = {
            (title:String,color:UIColor) in
            self.title = title
            self.view.backgroundColor = color
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
SubClosureViewController.swift中
class SubClosureViewController: UIViewController {

    //定义一个闭包
    var changeTitleAndClosure:((_ title:String,_ color:UIColor) -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myView()
    }
    func myView() {
        self.view.backgroundColor = UIColor.red
        
        let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30))
        tf.borderStyle = UITextBorderStyle.roundedRect
        tf.tag = 100
        self.view.addSubview(tf)
        
        let btn = UIButton(type:UIButtonType.system) as UIButton
        btn.frame = CGRect(x:60,y:100,width:100,height:30)
        btn.backgroundColor = UIColor.lightGray
        btn.setTitle("返回上一界面", for: UIControlState.normal)
        self.view.addSubview(btn)
        
        btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
    }
    func click(){
        let tf = self.view.viewWithTag(100) as? UITextField
        changeTitleAndClosure?(tf!.text!,UIColor.green)
        self.navigationController?.popViewController(animated: true)
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值