list - SwiftUI - List / ForEach 结合 NavigationLink 和 isActive 无法正常工作
问题描述
我正在尝试在 SwiftUI 的 List 或 ForEach 循环中执行 NavigationLink。不幸的是,我遇到了一个非常奇怪的行为(例如,当点击 Leo 时,它会打开 Karl,Opening Max 也指向 Karl)。
我已经发现它与 NavigationLink 中的“isActive”属性有关。不幸的是,我需要它在这里实现这种行为:https ://i.stack.imgur.com/g0BFz.gif在这里也被问到SwiftUI - Nested NavigationView: Go back to root。
我也尝试使用选择和标签属性,但我无法实现“回到根”机制。
这是示例:
import SwiftUI
struct Model: Equatable, Hashable {
var userId: String
var firstName: String
var lastName: String
}
struct ContentView: View {
@State var navigationViewIsActive: Bool = false
var myModelArray: [Model] = [
Model(userId: "27e880a9-54c5-4da1-afff-05b4584b1d2f", firstName: "Leo", lastName: "Test"),
Model(userId: "1050412a-cb12-4160-b7e4-2702ab8430c3", firstName: "Max", lastName: "Test"),
Model(userId: "1050412a-cb12-4160-b7e4-2702ab8430c3", firstName: "Karl", lastName: "Test")]
var body: some View {
NavigationView {
List(myModelArray, id: \.self) { model in
NavigationLink(destination: secondView(firstName: model.firstName), isActive: $navigationViewIsActive){ Text(model.firstName) }
}
.listStyle(PlainListStyle())
}
}
}
struct secondView: View {
@State var firstName: String
var body: some View {
NavigationView {
Text(firstName)
.padding()
}
}
}
谢谢!
解决方案
这是因为只使用了一种状态navigationViewIsActive
因此,当您单击导航链接时,该值将变为True
,并且所有链接都将变为active
这种情况的解决方案是这样的:
- 定义一个新的
State
,它将保存选定的模型值 - 你只需要一个
NavigationLink
,然后制作它Hidden
(把它放在 a 里面VStack
) - 在
List
使用Button
中代替NavigationLink
- 单击a 时
Button
:首先更改selectedModel
值,然后使 navigationLink 处于活动状态(true)
就像下面的代码 ( Tested with IOS 14
) :
import SwiftUI
struct Model: Equatable, Hashable {
var userId: String
var firstName: String
var lastName: String
}
struct ContentView: View {
@State var navigationViewIsActive: Bool = false
@State var selectedModel : Model? = nil
var myModelArray: [Model] = [
Model(userId: "27e880a9-54c5-4da1-afff-05b4584b1d2f", firstName: "Leo", lastName: "Test"),
Model(userId: "1050412a-cb12-4160-b7e4-2702ab8430c3", firstName: "Max", lastName: "Test"),
Model(userId: "1050412a-cb12-4160-b7e4-2702ab8430c3", firstName: "Karl", lastName: "Test")]
var body: some View {
NavigationView {
VStack {
VStack {
if selectedModel != nil {
NavigationLink(destination: SecondView(firstName: selectedModel!.firstName), isActive: $navigationViewIsActive){ EmptyView() }
}
}.hidden()
List(myModelArray, id: \.self) { model in
Button(action: {
self.selectedModel = model
self.navigationViewIsActive = true
}, label: {
Text(model.firstName)
})
}
.listStyle(PlainListStyle())
}
}
}
}
struct SecondView: View {
@State var firstName: String
var body: some View {
NavigationView {
Text(firstName)
.padding()
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
PS:我写的是:如何使用 SwiftUI 导航,它将帮助您了解在 swiftUI 中导航的方式
推荐阅读
- python - 线程数据共享
- delphi - Delphi IWinHTTPRequest SetRequestHeader
- python - 正则表达式在更多条件下匹配版权声明中的公司名称
- python - 我在使用形态掩码时遇到错误
- html - 将 div 垂直和水平居中
- c# - 编译服务此请求所需的资源期间发生错误。- DLL 错误
- eclipse - Eclipse IDE - 层次结构树中符号的含义
- python - 在没有某些字段的情况下查询 MongoDB 中的列表
- html - 使用滚动捕捉和位置粘性进行堆栈滚动
- php - PHP,SQL:如何在查询中使用 WHERE 字段来获取多个值?