Friday, November 6, 2026
HomeiOS Developmentios - Is delegate sample an excellent method in SwiftUI?

ios – Is delegate sample an excellent method in SwiftUI?

[ad_1]

I’m changing an iOS app from UIKit to SwiftUI. I’ve a UIViewController that shows a view with totally different buttons. This UIViewController has a delegate that conforms to a particular protocol. On every button’s motion, I name a way of its delegate. Nothing particular right here, it’s a typical Delegate sample as everyone knows for iOS growth with UIKit.

My query is: Is it nonetheless an excellent method with SwiftUI? I can completely convert this sample in my SwiftUI app (I did already. EDIT: and as talked about in feedback, it’s NOT a good suggestion!). However I am questioning if this Delegate sample continues to be a great way or if there’s a totally different means to try this (with Binding possibly?). Is it advisable to make use of this sample in a SwiftUI app?

Here’s a simplified code instance:

protocol MyCustomViewDelegate {
    func buttonATapped()
}

struct MyCustomView: View {
    public var delegate: MyCustomViewDelegate?
    var physique: some View {
        Button("Button A") {
            if let delegate = delegate {
                delegate.buttonATapped()
            }
        }
    }
}

struct ContentView: View, MyCustomViewDelegate {

    var physique: some View {
        MyCustomView(delegate: self)
    }

    func buttonATapped() {
        // Do one thing
    }
}

EDIT: Please don’t use this earlier implementation!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments