我嘗試在系結值更改時更新 UIView(在 UIViewRepresentable 內),但我不知道如何捕獲系結值的更改來更新 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 ...
}
uj5u.com熱心網友回復:
你不需要使用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
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480813.html
標籤:IOS 迅速 迅捷 uikit uiviewrepresentable
上一篇:如何過濾具有獨特屬性的物件陣列?
