I am converting an iOS app from UIKit to SwiftUI. I have a UIViewController that displays a view with different buttons. This UIViewController 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.
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?
Here is a simplified code example:
protocol MyCustomViewDelegate {
func buttonATapped()
}
struct MyCustomView: View {
public var delegate: MyCustomViewDelegate?
var body: some View {
Button("Button A") {
if let delegate = delegate {
delegate.buttonATapped()
}
}
}
}
struct ContentView: View, MyCustomViewDelegate {
var body: some View {
MyCustomView(delegate: self)
}
func buttonATapped() {
// Do something
}
}
EDIT: Please do not use this previous implementation!