SwiftUI ForEach refresh makes view pop

只愿长相守 提交于 2021-01-28 13:34:18

问题


I have something like this:


import SwiftUI

struct Overview: View {

    @ObservedObject var firstArray = FirstArray()

    var body: some View {
        Group{
            ScrollView(){
                ForEach(self.firstArray.array, id: \.self){ array in
                    SubView(array: array)
                }
                Spacer()
            }
        }.navigationBarTitle("Overview")
    }
}


struct SubView: View {

    @State var array

    var body: some View {
           NavigationLink(destination: DetailInfo(array: array){
                Image(systemName: "pencil.circle").resizable().frame(width: 30, height: 30)
            }
    }.navigationBarTitle("SubView")
}


struct DetailInfo: View {

    @State var array

    var body: some View {
        List(){
            ForEach(0..<self. array.count, id: \.self){item in
                Text(item)
            }.onDelete(perform: deleteItem)
        }
    }

    func deleteItem(at offsets: IndexSet) {
       self.array.remove(atOffsets: offsets)
    }

}

When I delete an item in the DetailInfo it pops me back to the Overview. Why is that? Is that because of the ForEach "refreshing" in the Overview. How can I stay on the DetailInfo ? When leaving out the id: \.self in Overview it doesn't show this behaviour.


回答1:


yes, it is because everything gets refresh and so the detail view and the navigationlink won't exist anymore and so the app "jumps" back to the root view

the "normal" solution is: do not delete the item in the detail view but in the list - then there is no problem and you got a nice animation for free from SwiftUI too



来源:https://stackoverflow.com/questions/61987960/swiftui-foreach-refresh-makes-view-pop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!