swift-SwiftUI macOS NavigationView-onChange(of:Bool)操作尝试每帧更新多次
发布时间:2022-03-07 19:49:35 190
相关标签: # react-native
我看到了onChange(of: Bool) action tried to update multiple times per frame
单击时出现警告NavigationLink
它位于SwiftUI macOS应用程序的侧边栏中。
以下是我目前拥有的:
import SwiftUI
@main
struct BazbarApp: App {
@StateObject private var modelData = ModelData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(modelData)
}
}
}
class ModelData: ObservableObject {
@Published var myLinks = [URL(string: "https://google.com")!, URL(string: "https://apple.com")!, URL(string: "https://amazon.com")!]
}
struct ContentView: View {
@EnvironmentObject var modelData: ModelData
@State private var selected: URL?
var body: some View {
NavigationView {
List(selection: $selected) {
Section(header: Text("Bookmarks")) {
ForEach(modelData.myLinks, id: \.self) { url in
NavigationLink(destination: DetailView(selected: $selected) ) {
Text(url.absoluteString)
}
.tag(url)
}
}
}
.onDeleteCommand {
if let selected = selected {
modelData.myLinks.remove(at: modelData.myLinks.firstIndex(of: selected)!)
}
selected = nil
}
Text("Choose a link")
}
}
}
struct DetailView: View {
@Binding var selected: URL?
var body: some View {
if let selected = selected {
Text("Currently selected: \(selected)")
}
else {
Text("Choose a link")
}
}
}
当我交替点击侧边栏中的第二和第三个链接时,我最终开始在控制台中看到上述警告。
下面是我所指内容的gif:
有趣的是,在第一个和第二个链接之间交替单击时,警告不会出现。
有人知道怎么解决这个问题吗?
我正在使用macOS 12.2.1&;代码13.2.1。
提前谢谢
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报