[Swift] Vaguely grasp the flow of Delegate

What is delegation?

It's getting complicated when I think about it, but I vaguely grasp the flow.

Delegation side

** side that asks ** to use the method ".

NextViewController.swift


// 1.Set rules
protocol CatchProtocol {
  func catchData(count:Int)
}

class NextViewController: UIViewController {

  var count:Int = 0

  // 2.Make 1 a variable named deledele
  var deledele:CatchProtocol?

  @IBAction func push(_ sender: Any) {
    
    // 3.Activate. Pass arguments if needed
    deledele?.catchData(count:count)
  }

Contractor

The ** side who is asked ** to use the method.

ViewController.swift


// 1.declare
class ViewController: UIViewController, CatchProtocol {

  @IBOutlet weak var label: UILabel!
  
  var count:Int = 0
  
  override func viewDidLoad() {
    super.viewDidLoad()
  }

  // 2.Delegate method. Created by pressing fix with an error when declaring
  func catchData(count: Int) {
    label.text = String(count)
  }
  
  @IBAction func next(_ sender: Any) {
    performSegue(withIdentifier: "next", sender: nil)
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let nextVC = segue.destination as! NextViewController

    //The description "I was entrusted with the deledele of NextViewController"
    nextVC.deledele = self
  }
}

――It's really easier to understand when other people such as team members see it if you use delegate instead of deledele. ――I understand the flow somehow, but the last self is vague

Recommended Posts

[Swift] Vaguely grasp the flow of Delegate
[Swift] This is the solution! Illustration of Delegate implementation
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
[Swift] Termination of the program by assertion
[Swift] Get the height of Safe Area
[Swift] Change the color of SCN Node
The basic basis of Swift custom cells
[Swift] Termination of the program by the fatalError function
[Swift] Get the number of steps with CMP edometer
[Swift] Why FlowLayout should use Delegate instead of instance
Find the approximate value of log (1 + x) in Swift
Roughly the flow of web application development with Rails.
[Swift] How to get the document ID of Firebase
Control the processing flow of Spring Batch with JavaConfig.
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
The idea of jQuery
Now, I understand the coordinate transformation method of UIView (Swift)
[Swift] I tried to implement the function of the vending machine
[Swift] Lightly introduce the logic of the app that passed the selection
Let's grasp the operation image (atmosphere) of the DI container of Spring
[Swift] Get the timing when the value of textField is changed
Aiming for a basic understanding of the flow of recursive processing