@binding值更改时更新Uiview(uiviewReprestable Inside)

发布于 2025-01-31 06:21:11 字数 1575 浏览 5 评论 0原文

当绑定值更改时,我尝试更新Uiview(在UiviewMersentable内部),但是我不知道如何捕获绑定值的更改以更新Uiview?我在接收函数上使用了一条警告消息,说明了

示例代码:

struct CustomPickerView: UIViewRepresentable {
    
    let data: [String]
    @Binding var selectedValue: String
    
    //makeCoordinator()
    func makeCoordinator() -> CustomPickerView.Coordinator {
        return CustomPickerView.Coordinator(self)
    }

    //makeUIView(context:)
    func makeUIView(context: UIViewRepresentableContext<CustomPickerView>) -> UIPickerView {
        let picker = UIPickerView(frame: .zero)
        // allows rows to be compressed in swiftUI
        picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        picker.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
        // allows rows to be expanded
        picker.setContentHuggingPriority(.defaultLow, for: .horizontal)
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        
        picker.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: false)
        return picker
    }

    //updateUIView(_:context:)
    func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<CustomPickerView>) {

        onReceive(selectedValue.publisher) { newValue in

       // I try to update the picker view selected row when binding value changes...

            view.selectRow(data.firstIndex(of: newValue.debugDescription) ?? 0, inComponent: 0, animated: true)
        }
    }

// The rest of code ...

}

I try to update UIView (inside UIViewRepresentable) when the binding value changes, but I don't know how to catch the changes of the binding value to update the UIView? I used on receive function but a warning message showed say (the result of the call on receive unused)

The sample code:

struct CustomPickerView: UIViewRepresentable {
    
    let data: [String]
    @Binding var selectedValue: String
    
    //makeCoordinator()
    func makeCoordinator() -> CustomPickerView.Coordinator {
        return CustomPickerView.Coordinator(self)
    }

    //makeUIView(context:)
    func makeUIView(context: UIViewRepresentableContext<CustomPickerView>) -> UIPickerView {
        let picker = UIPickerView(frame: .zero)
        // allows rows to be compressed in swiftUI
        picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        picker.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
        // allows rows to be expanded
        picker.setContentHuggingPriority(.defaultLow, for: .horizontal)
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        
        picker.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: false)
        return picker
    }

    //updateUIView(_:context:)
    func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<CustomPickerView>) {

        onReceive(selectedValue.publisher) { newValue in

       // I try to update the picker view selected row when binding value changes...

            view.selectRow(data.firstIndex(of: newValue.debugDescription) ?? 0, inComponent: 0, animated: true)
        }
    }

// The rest of code ...

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2025-02-07 06:21:11

您无需使用onReceive,而是观看发布者 - updateUiview将在selectedvalue更改时调用,因此您可以直接使用它:

struct ContentView: View {
    @State private var selected = "3"
    
    var body: some View {
        CustomPickerView(data: ["1","2","3"], selectedValue: $selected)
        Button("Choose 1") {
            selected = "1"
        }
    }
}

struct CustomPickerView: UIViewRepresentable {
    
    let data: [String]
    @Binding var selectedValue: String
    
    //makeCoordinator()
    func makeCoordinator() -> CustomPickerView.Coordinator {
        return CustomPickerView.Coordinator(self)
    }

    //makeUIView(context:)
    func makeUIView(context: UIViewRepresentableContext<CustomPickerView>) -> UIPickerView {
        let picker = UIPickerView(frame: .zero)
        // allows rows to be compressed in swiftUI
        picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        picker.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
        // allows rows to be expanded
        picker.setContentHuggingPriority(.defaultLow, for: .horizontal)
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        
        picker.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: false)
        return picker
    }

    func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<CustomPickerView>) {
        view.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: true)
    }
    
    class Coordinator : NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            1
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            parent.data.count
        }

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
        {
            parent.data[row]
        }
        
        var parent: CustomPickerView
        
        init(_ input : CustomPickerView) {
            self.parent = input
        }
    }
}

You don't need to use onReceive and watch the publisher -- updateUIView will be called when selectedValue changes, so you can use it directly:

struct ContentView: View {
    @State private var selected = "3"
    
    var body: some View {
        CustomPickerView(data: ["1","2","3"], selectedValue: $selected)
        Button("Choose 1") {
            selected = "1"
        }
    }
}

struct CustomPickerView: UIViewRepresentable {
    
    let data: [String]
    @Binding var selectedValue: String
    
    //makeCoordinator()
    func makeCoordinator() -> CustomPickerView.Coordinator {
        return CustomPickerView.Coordinator(self)
    }

    //makeUIView(context:)
    func makeUIView(context: UIViewRepresentableContext<CustomPickerView>) -> UIPickerView {
        let picker = UIPickerView(frame: .zero)
        // allows rows to be compressed in swiftUI
        picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        picker.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
        // allows rows to be expanded
        picker.setContentHuggingPriority(.defaultLow, for: .horizontal)
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        
        picker.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: false)
        return picker
    }

    func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<CustomPickerView>) {
        view.selectRow(data.firstIndex(of: selectedValue) ?? 0, inComponent: 0, animated: true)
    }
    
    class Coordinator : NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            1
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            parent.data.count
        }

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
        {
            parent.data[row]
        }
        
        var parent: CustomPickerView
        
        init(_ input : CustomPickerView) {
            self.parent = input
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文