NavigationLink freezes when trying to revisit previously clicked NavigationLink in SwiftUI

后端 未结 1 1748
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 01:12

I am designing an app that includes the function of retrieving JSON data and displaying a list of retrieved items in a FileBrowser type view. In this view, a user should be able

1条回答
  •  心在旅途
    2021-02-06 01:51

    Had the same issue - try this. I would call this a hack to be removed when the bug in swiftUI is corrected.

    struct ListView: View {
    @State private var destID = 0
    ...
    var body: some View {
    ...
      NavigationLink(destination: FileView(path: "/\(cell.FileID)", displaysLogin: self.$displaysLogin)
       .navigationBarTitle(cell.FileName) 
       .onDisappear() { self.destID = self.destID + 1 }
      ){
       constructedCell(FileType: cell.FileType, FileName: cell.FileName, FileID: cell.FileID) 
      }.id(destID)
    

    Essentially it seems that in some circumstances (iOS 13.3 - Simulator?) the NavigationLink is not reset when the destination view is removed from the navigation stack. As a work around we need to regenerate the Navigation Link. This is what changing the id does. This corrected my issue.

    However if you have NavigationLinks that are chained, that is a link that leads to another list of links, then this solution will create side effects; the stack returns to the origin at the second attempt to show the last view.

    0 讨论(0)
提交回复
热议问题