NavigationStack(path: $presentedParks) { List(parks) { park in NavigationLink(park.name, value: park) } .navigationDestination(for: Park.self) { park in ParkDetails(park: park) } }
NavigationStack(path: $presentedPaths) { List { ForEach(parks) { park in NavigationLink(park.name, value: park) } ForEach(zoos) { zoo in NavigationLink(zoo.name, value: zoo) } } .navigationDestination(for: Park.self) { park in ParkDetails(park: park) } .navigationDestination(for: Zoo.self) { zoo in ZooDetails(park: zoo) } }
NavigationPath也支持简单的数据添加和删除元素的等操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/// Appends a new value to the end of this path. publicmutatingfuncappend<V>(_value: V) whereV : Hashable
/// Appends a new codable value to the end of this path. publicmutatingfuncappend<V>(_value: V) whereV : Decodable, V : Encodable, V : Hashable
/// Removes values from the end of this path. /// /// - Parameters: /// - k: The number of values to remove. The default value is `1`. /// /// - Precondition: The input parameter `k` must be greater than or equal /// to zero, and must be less than or equal to the number of elements in /// the path. publicmutatingfuncremoveLast(_k: Int=1)
enumAppViewRouter: Hashable { enumCategory: Hashable { case list case detail(Int) } case profile case category(Category) var title: String { switchself { case .profile: return"Profile" case .category(let category): switch category { case .list: return"Category List" case .detail(let id): return"Category Detail: \(id)" } } } }