이전 화면으로 데이터 전달(클로저 사용)
- 데이터를 전달 받을 ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var dataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapNextBarButton(_ sender: UIBarButtonItem) {
guard let nextViewController = self.storyboard?.instantiateViewController(identifier: "NextViewController") as? NextViewController else { return }
nextViewController.modalPresentationStyle = .fullScreen
nextViewController.dataSendClosure = { data in
self.dataLabel.text = data
}
self.present(nextViewController, animated: true, completion: nil)
}
}
import UIKit
class NextViewController: UIViewController {
var dataSendClosure: ((_ data: String) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapNoDataButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func tapYesDataButton(_ sender: UIButton) {
self.dataSendClosure?("이전 화면에서 전달 받음")
self.dismiss(animated: true, completion: nil)
}
}