ViewController之间通过协议传值

//
//  TestViewController.swift
//  MyTest
//
//  Created by riku on 2018/3/28.
//  Copyright © 2018年 riku. All rights reserved.
//

import UIKit
// 先定义个protocol,用来表示delegate
protocol TestBackDelegate {
    func onNavBack(_ backParam: String)
}

class TestViewController: UIViewController {
    
    var params: String?
    var delegate: TestBackDelegate?
    let button = UIButton(type: UIButtonType.system)
    
    // 关闭方法
    @objc func close() {
        self.dismiss(animated: true, completion: nil)
        // 委托 delegate 传递参数
        delegate?.onNavBack("backParam")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 使用Auto Layout的方式来布局
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("close", for: UIControlState())
        button.tintColor = UIColor.white
        button.backgroundColor = UIColor.brown
        
        // 按钮绑定点击事件
        button.addTarget(self, action: #selector(TestViewController.close), for: UIControlEvents.touchUpInside)

        self.view.addSubview(button)
        
        // 添加子视图之后添加约束,使按钮居中
        self.view.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0.0)
        )
        self.view.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0.0)
        )
        self.view.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .width,
            multiplier: 0.0,
            constant: 100.0)
        )
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        print("\(params ?? "no params")")
    }


}

//
//  ViewController.swift
//  MyTest
//
//  Created by riku on 2018/3/16.
//  Copyright © 2018年 riku. All rights reserved.
//

import UIKit

class ViewController: UIViewController, TestBackDelegate {
    
    let button = UIButton(type: UIButtonType.system)
    
    func onNavBack(_ backParam: String) {
        print(backParam)
    }
    
    @objc func showModal(_: AnyObject) {
        let vc = TestViewController()
        // 过渡效果
        vc.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        vc.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
        vc.params = "传参数"
        vc.delegate = self
        self.present(vc, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        button.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
        button.setTitle("show modal", for: .normal)
        button.addTarget(
            self, action:
            #selector(ViewController.showModal(_:)),
            for: UIControlEvents.touchUpInside
        )
        self.view.addSubview(button)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

推荐阅读更多精彩内容