简体   繁体   English

代表 SwiftUI 从托管视图 Controller 查看

[英]Delegate for SwiftUI View from Hosting View Controller

I add UIHostingViewController to my Storyboard and make class for this.我将 UIHostingViewController 添加到我的 Storyboard 并为此制作 class 。 Load mySwiftUIView in controller init.在 controller init 中加载 mySwiftUIView。 Everything works good.一切正常。

But i want to make hosting controller like delegate for mySwiftUIView because I want to handle button pressing in view.但我想让托管 controller 像 mySwiftUIView 的委托一样,因为我想在视图中处理按钮按下。

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. Id 不起作用,因为我在托管 controller 完全初始化之前通过了视图。 Swift shows message "self used before super.init call" Swift 显示消息“self used before super.init call”

If I will use usual UIViewController and put HostingController inside - it is works.如果我将使用通常的 UIViewController 并将 HostingController 放入其中 - 它是有效的。 But this is not suitable for me because it calls problem with sizes and navigation.但这不适合我,因为它涉及尺寸和导航问题。

How can I do it with separate UIHostingController.如何使用单独的 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)
}
}

Since MyHostingViewController knows that its rootView is MySwiftUIView , you could assign the view controller as the delegate afterwards:由于MyHostingViewController知道它的rootViewMySwiftUIView ,因此您可以在之后将视图 controller 分配为委托:

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

You can wrap delegate into class您可以将委托包装到 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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM