gpt4 book ai didi

ios - onDismiss() 在绑定(bind)对象更新之前被调用

转载 作者:行者123 更新时间:2023-12-04 08:32:49 25 4
gpt4 key购买 nike

我不太确定我在标题中使用了正确的短语,但这就是我正在努力实现的目标。我有一个带有按钮的页面,该按钮显示 DocumentPicker 以允许用户从他们的文档目录中选择项目。我已经像这样包装了 UIDocumentPickerViewController:

struct DocumentPicker: UIViewControllerRepresentable {

class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: DocumentPicker

init(_ parent: DocumentPicker) {
self.parent = parent
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
parent.urls = urls
parent.presentationMode.wrappedValue.dismiss()
}
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

@Environment(\.presentationMode) var presentationMode
@Binding var urls: [URL]?

func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.movie])
pickerViewController.allowsMultipleSelection = true
pickerViewController.delegate = context.coordinator
return pickerViewController
}

func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) { }
}

我的观点是这样的:

struct VideoList: View {

@ObservedObject var playlistStore = PlaylistStore()

@State private var showingDocumentPicker = false
@State private var urls: [URL]?

// MARK: - View

var body: some View {

NavigationView {
List(playlistStore.items) { item in
Text(item.title)
}
.navigationBarItems(trailing: Button("Add Items") { self.showingDocumentPicker = true })
}
.sheet(isPresented: $showingDocumentPicker, onDismiss: updatePlaylistStore) {
DocumentPicker(urls: self.$urls)
}
}

private func updatePlaylistStore() {
guard let urls = urls else { return }
playlistStore.addItems(items: urls)
self.urls = nil
}
}

当我选择项目并且工作表关闭时出现问题。 onDismiss 被调用,然后调用 updatePlaylistStore。但是,此时我刚刚选择的 URL 尚不可用。如果我再次添加更多项目,当第二次调用 onDismiss 时,我第一次选择的项目可用。由于我使用的是 DocumentPicker(urls: self.$urls) 我希望 URL 在被选择时立即更新。我在上下文中留下了一个名为 PlaylistStore() 的对象,但我一直在尝试打印 urls 的值,然后再添加到商店以检查我的问题与 PlaylistStore() 无关。谁能发现我做错了什么?任何指导表示赞赏!

最佳答案

有问题的代码基于对某些顺序或调用的假设,但没有关于该顺序的 promise 。

相反,我们应该只对发生的变化使用react。

这里是固定变体。使用 Xcode 12.1/iOS 14.1 测试

struct VideoList: View {

@ObservedObject var playlistStore = PlaylistStore()

@State private var showingDocumentPicker = false
@State private var urls: [URL]?

// MARK: - View

var body: some View {

NavigationView {
List(playlistStore.items) { item in
Text(item.title)
}
.navigationBarItems(trailing: Button("Add Items") { self.showingDocumentPicker = true })
}
.sheet(isPresented: $showingDocumentPicker) { // no needs in onDismiss
DocumentPicker(urls: self.$urls)
}
.onChange(of: urls) { _ in // update when urls changed !!
updatePlaylistStore()
}
}

private func updatePlaylistStore() {
guard let urls = urls else { return }
playlistStore.addItems(items: urls)
self.urls = nil
}
}

关于ios - onDismiss() 在绑定(bind)对象更新之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64941667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com