Delegate for SwiftUI View from Hosting View Controller

4.6k views Asked by At

I add UIHostingViewController to my Storyboard and make class for this. Load mySwiftUIView in controller init. Everything works good.

But i want to make hosting controller like delegate for mySwiftUIView because I want to handle button pressing in view.

required init?(coder: NSCoder) {
    var mySwiftView = MySwiftUIView()
    mySwiftView.delegate = self //self used before super.init call
    super.init(coder: coder,rootView: mySwiftView)
}

Id doesn't work because I pass view before hosting controller fully init. Swift shows message "self used before super.init call"

If I will use usual UIViewController and put HostingController inside - it is works. But this is not suitable for me because it calls problem with sizes and navigation.

How can I do it with separate UIHostingController. Thanks

Full code

import UIKit
import SwiftUI

protocol MySwiftUIViewDelegate{
    func buttonPressed()
}

struct MySwiftUIView: View {
    var delegate: MySwiftUIViewDelegate?
    
    var body: some View {
        Button(action: {
            delegate?.buttonPressed()
        }) {
            Text("My button")
        }
    }
}

class MyHostingViewController: UIHostingController<MySwiftUIView>, MySwiftUIViewDelegate {
        
required init?(coder: NSCoder) {
    var mySwiftView = MySwiftUIView()
    //mySwiftView.delegate = self //self used before super.init call
    super.init(coder: coder,rootView: mySwiftView)
}

override func viewDidLoad() {
    super.viewDidLoad()
}

func buttonPressed() {
    performSegue(withIdentifier: "GoToOtherScreenSegue", sender: self)
}
}
2

There are 2 answers

1
EDUsta On BEST ANSWER

Since MyHostingViewController knows that its rootView is MySwiftUIView, you could assign the view controller as the delegate afterwards:

required init?(coder: NSCoder) {
    var mySwiftView = MySwiftUIView()
    super.init(coder: coder, rootView: mySwiftView)
    rootView.delegate = self
}
0
Konstantin Zyryanov On

You can wrap delegate into class

import UIKit
import SwiftUI

protocol MySwiftUIViewDelegate{
    func buttonPressed()
}

struct MySwiftUIView: View {
    let configuration: Configuration
    
    var body: some View {
        Button(action: {
            configuration.delegate?.buttonPressed()
        }) {
            Text("My button")
        }
    }
}

extension MySwiftUIView {
    final class Configuration {
        unowned var delegate: MySwiftUIViewDelegate?
    }
}

class MyHostingViewController: UIHostingController<MySwiftUIView>, MySwiftUIViewDelegate {
        
    required init?(coder: NSCoder) {
        let configuration = MySwiftUIView.Configuration()
        let mySwiftView = MySwiftUIView(configuration: configuration)
        super.init(coder: coder,rootView: mySwiftView)
        configuration.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func buttonPressed() {
        performSegue(withIdentifier: "GoToOtherScreenSegue", sender: self)
    }
}