title: Is delegate pattern a good approach in SwiftUI?
body: <p>I am converting an iOS app from UIKit to SwiftUI. I have a <code>UIViewController</code> that displays a view with different buttons. This <code>UIViewController</code> has a delegate that conforms to a specific protocol. On each button's action, I call a method of its delegate. Nothing special here, it is a typical Delegate pattern as we all know for iOS development with UIKit.</p>
  <p>My question is: Is it still a good approach with SwiftUI? I can totally convert this pattern in my SwiftUI app (I did already. EDIT: and as mentioned in comments, it is NOT a good idea!). But I'm wondering if this Delegate pattern is still a good way or if there is a different way to do that (with Binding maybe?). Is it recommended to use this pattern in a SwiftUI app?</p>
  <p>Here is a simplified code example:</p>
  <pre class="lang-swift prettyprint-override"><code>protocol MyCustomViewDelegate {
      func buttonATapped()
  }
  
  struct MyCustomView: View {
      public var delegate: MyCustomViewDelegate?
      var body: some View {
          Button(&quot;Button A&quot;) {
              if let delegate = delegate {
                  delegate.buttonATapped()
              }
          }
      }
  }
  
  struct ContentView: View, MyCustomViewDelegate {
  
      var body: some View {
          MyCustomView(delegate: self)
      }
  
      func buttonATapped() {
          // Do something
      }
  }
  </code></pre>
  <p>EDIT: Please do not use this previous implementation!</p>