在推送视图中修改列表绑定 属性 时 SwiftUI 导航弹回

SwiftUI Navigation popping back when modifying list binding property in a pushed view

当我从向下 2+ 层的推送视图中的数组更新绑定 属性 时,导航会在更改为 属性 后立即弹出。

Xcode 13.3 测试版,iOS 15.

我创建了一个简单的演示,代码如下。

Shopping Lists List Edit List section Edit

更新列表标题(一个视图深度)没问题,导航堆栈保持不变,如果我 return,更改会发布。但是当调整一个部分标题(两个深)时,只要我对 属性.

进行一次更改,导航就会弹出。

我觉得我缺少基本的基础知识,我觉得它一定与列表有关 id?但我正在努力弄清楚或解决它。

GIF

代码:

型号:

struct ShoppingList {
    let id: String = UUID().uuidString
    var title: String
    var sections: [ShoppingListSection]
}

struct ShoppingListSection {
    let id: String = UUID().uuidString
    var title: String
}

查看模型:

final class ShoppingListsViewModel: ObservableObject {
    @Published var shoppingLists: [ShoppingList] = [
        .init(
            title: "Shopping List 01",
            sections: [
                .init(title: "Fresh food")
            ]
        )
    ]
}

内容视图:

struct ContentView: View {
    var body: some View {
        NavigationView {
            ShoppingListsView()
        }
    }
}

ShoppingListsView

struct ShoppingListsView: View {
    @StateObject private var viewModel = ShoppingListsViewModel()

    var body: some View {
        List($viewModel.shoppingLists, id: \.id) { $shoppingList in
            NavigationLink(destination: ShoppingListEditView(shoppingList: $shoppingList)) {
                Text(shoppingList.title)
            }
        }
        .navigationBarTitle("Shopping Lists")
    }
}

ShoppingListEditView

struct ShoppingListEditView: View {
    @Binding var shoppingList: ShoppingList

    var body: some View {
        Form {
            Section(header: Text("Title")) {
                TextField("Title", text: $shoppingList.title)
            }
            Section(header: Text("Sections")) {
                List($shoppingList.sections, id: \.id) { $section in
                    NavigationLink(destination: ShoppingListSectionEditView(section: $section)) {
                        Text(section.title)
                    }
                }
            }
        }
        .navigationBarTitle("Edit list")
    }
}

ShoppingListSectionEditView

struct ShoppingListSectionEditView: View {
    @Binding var section: ShoppingListSection

    var body: some View {
        Form {
            Section(header: Text("Title")) {
                TextField("title", text: $section.title)
            }
        }
        .navigationBarTitle("Edit section")
    }
}

试试这个,对我有用:

struct ContentView: View {
    var body: some View {
        NavigationView {
            ShoppingListsView()
        }.navigationViewStyle(.stack)  // <--- here
    }
}

尝试让您反对确认 Identifiable 和 return 值是唯一且稳定的,对于您的情况是 ShoppingList.

当对象 id 更改时,详细视图似乎会弹出。