Advertisement

SwiftUI 问答之 将数据从 SwiftUI 传递到 UIKit 的回调

阅读量:

实战需求

在Swift视图的回调关闭过程中如何实现数据传输至UikitViewController?假设我们有一个Swift视图:

复制代码
    import SwiftUI
    
    struct MyView: View {
    var buttonPressed: (() -> Void)?
    @State var someData = ""
    
    var body: some View {
        ZStack {
            Color.purple
            Button(action: {
                someData = "new Data"
                self.buttonPressed?()
                
            }) {
                Text("Button")
            }
        }
    }
    }
    
    struct MyView_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
    }
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解读

和 ViewController 在哪里,其中我们有 SwiftUI 视图:

复制代码
    import UIKit
    import SwiftUI
    
    class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let swiftUIView = MyView()
        let hostingViewController = UIHostingController(rootView: swiftUIView)
        self.view.addSubview(hostingViewController.view)
        
        hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingViewController.view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        hostingViewController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        hostingViewController.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        hostingViewController.view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        
        hostingViewController.rootView.buttonPressed = {
            print ("callback recived")
            // i know i can try to get the data in this way, but if MyView become too complex than it won't look well
            //print(hostingViewController.rootView.$someData)
        }
    
    }
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解读

如何通过闭包向 ViewController 发送一些数据?

解放方案

您可以通过参数传递它,例如

复制代码
    struct MyView: View {
    var buttonPressed: ((String) -> Void)? // << here !!
    @State var someData = ""
    
    var body: some View {
        ZStack {
            Color.purple
            Button(action: {
                someData = "new Data"
                self.buttonPressed?(someData)  // << here !!
    
    
      
      
      
      
      
      
      
      
      
      
    
    代码解读

复制代码
    hostingViewController.rootView.buttonPressed = { value in // << here !!
        print("callback received")
        print(value)
    }
    
    
      
      
      
      
    
    代码解读

加入我们一起学习SwiftUI

QQ号:3365059189
SwiftUI技术交流群的QQ号码:518696470
官方网站地址:https://www.openswiftui.com

全部评论 (0)

还没有任何评论哟~