NavigationLink 在 macOS 下除 List 之外的其他设备上是否可以正常运行

发布于 2025-01-10 03:36:42 字数 2706 浏览 0 评论 0原文

有没有人得到了一个 macOS 示例,其中除了 List 之外的任何内容中的 navigationLink 都可以按预期工作?

在一些测试中,我发现如果使用列表,它工作得很好,但是,在 ScrollView、VStack 或 LazyHGrid 中,它只会在每次单击时按预期更新目标。

下面是我的测试代码。 (忽略滑动的东西 - 但感谢 此处了解见解)。如果我使用 List 作为容器,详细信息视图将按预期显示。将列表替换为其他 3 个中的任何一个都会导致每秒单击一次都会生成空白详细信息。 IE 单击第一、第二、第三、第四,它会输出第一、空白、第三、空白。

这只是一个 macOS,“在 Apple 修复它之前一直使用它”还是我错过了一些基本的东西?

蒂亚·

艾伦

import SwiftUI

struct ContentView: View {
  enum SwipeHorizontalDirection: String {
      case left, right, none
  }
  @State private var swipeHorizontalDirection:SwipeHorizontalDirection = .none
  let rows = Array(repeating: GridItem(.fixed(100)), count: 6)
  let tiles = [
    TileView( title: "first"),
    TileView( title: "second"),
    TileView( title: "third"),
    TileView( title: "fourth")
  ]
  @State private var selectedTileID: UUID? = nil
  var body: some View {
    VStack {
      Text("A Test View")
      NavigationView {
//        VStack {
//        LazyHGrid(rows: rows) {
//        ScrollView {
        List {
        ForEach (0 ..< tiles.count) { index in
          NavigationLink(tiles[index].title, destination: tiles[index], tag: tiles[index].id, selection: $selectedTileID)
              }
        }
      }
    }
    .gesture(DragGesture()
              .onEnded{
      if $0.startLocation.x > $0.location.x {
          self.swipeHorizontalDirection = .left
      } else if $0.startLocation.x == $0.location.x {
          self.swipeHorizontalDirection = .none
      } else {
          self.swipeHorizontalDirection = .right
      }
      let nextID = nextItemAdjacentActive(direction: swipeHorizontalDirection)
      selectedTileID = nextID
    }
    )
  }

  func nextItemAdjacentActive(direction: SwipeHorizontalDirection) -> UUID
  {
    var foundIndex = 0
    for index in 0 ..< tiles.count {
      if tiles[index].id == selectedTileID {
        if direction == .right {
          if (index + 1) < tiles.count {
            foundIndex = index+1
          }
          else { foundIndex = 0}
        }
        else if direction == .left {
          if (index > 0) {
            foundIndex = index-1
          }
          else {
            foundIndex = tiles.count - 1
          }
        }
      }
    }
    return tiles[foundIndex].id
  }

}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

struct TileView: View, Identifiable {
  var id = UUID()
  var title:String
  
  var body: some View {
    VStack {
      Text(title).font(.largeTitle)
    }
  }
  
}


Has anyone got a working macOS example of a navigationLink within anything other than a List that works as expected ?

On some testing, I find that it works fine if it is with a List, however, in ScrollView, VStack or LazyHGrid it only updates the destination as expected on every second click.

Below is my test code. (ignore the swipe stuff - but thanks to here for the insights). If I use List as the container, the detail views appear as expected. Replacing List with either of the other 3 results in every second click producing a blank detail. I.E. click, first, second, third, fourth and it delivers first, blank, third, blank.

Is this just a macOS, "live with until Apple fixes it" or am I missing something fundamental ?

TIA

Alan

import SwiftUI

struct ContentView: View {
  enum SwipeHorizontalDirection: String {
      case left, right, none
  }
  @State private var swipeHorizontalDirection:SwipeHorizontalDirection = .none
  let rows = Array(repeating: GridItem(.fixed(100)), count: 6)
  let tiles = [
    TileView( title: "first"),
    TileView( title: "second"),
    TileView( title: "third"),
    TileView( title: "fourth")
  ]
  @State private var selectedTileID: UUID? = nil
  var body: some View {
    VStack {
      Text("A Test View")
      NavigationView {
//        VStack {
//        LazyHGrid(rows: rows) {
//        ScrollView {
        List {
        ForEach (0 ..< tiles.count) { index in
          NavigationLink(tiles[index].title, destination: tiles[index], tag: tiles[index].id, selection: $selectedTileID)
              }
        }
      }
    }
    .gesture(DragGesture()
              .onEnded{
      if $0.startLocation.x > $0.location.x {
          self.swipeHorizontalDirection = .left
      } else if $0.startLocation.x == $0.location.x {
          self.swipeHorizontalDirection = .none
      } else {
          self.swipeHorizontalDirection = .right
      }
      let nextID = nextItemAdjacentActive(direction: swipeHorizontalDirection)
      selectedTileID = nextID
    }
    )
  }

  func nextItemAdjacentActive(direction: SwipeHorizontalDirection) -> UUID
  {
    var foundIndex = 0
    for index in 0 ..< tiles.count {
      if tiles[index].id == selectedTileID {
        if direction == .right {
          if (index + 1) < tiles.count {
            foundIndex = index+1
          }
          else { foundIndex = 0}
        }
        else if direction == .left {
          if (index > 0) {
            foundIndex = index-1
          }
          else {
            foundIndex = tiles.count - 1
          }
        }
      }
    }
    return tiles[foundIndex].id
  }

}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

struct TileView: View, Identifiable {
  var id = UUID()
  var title:String
  
  var body: some View {
    VStack {
      Text(title).font(.largeTitle)
    }
  }
  
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文