无法在视图之间传递数据,也无法在第二个视图中更新 - SwiftUI
Cannot pass data between views and also update in second view - SwiftUI
我正在尝试在 this article 之后使用 @Published
和 @ObservedObject
在两个视图之间传递数据,但由于某些原因我无法传递数据, @Published
只是保持初始状态,而不是在单击按钮时更改它。
第一个视图 ProductList2
导航到一个 tabView,我试图将数据传递到其中一个 tabView。任何帮助将不胜感激。
这是我创建 ObservableObject
和 @Published
的视图
class selectedApplication: ObservableObject {
@Published var selectedApplication = "All"
}
struct ProductList2: View {
@ObservedObject var selectedOption = selectedApplication()
var products: [ProductModel] = productData
var body: some View {
VStack {
HStack {
List{
ForEach(applicationsArray, id: \.self) { item in
Button(action: {
self.selectedOption.selectedApplication = item
}) {
HStack(){
Image(item)
Text(item)
}
}
}
}
}
List{
let matchedItems = products.filter {
product in
let list = product.application
for item in list {
if item == selectedOption.selectedApplication {
return true
}
}
return false
}
ForEach(matchedItems) { item in
NavigationLink(destination: ProductTabView(product: item)) {
ProductListRow(product: item)
}
}
}
}
}
}
这是我尝试检索数据的选项卡视图:
struct ProductTab5View: View {
var product: ProductModel
@ObservedObject private var application = selectedApplication()
var body: some View {
VStack(alignment: .leading){
Text(product.detailTabNames[3])
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 0){
ForEach(product.application, id: \.self) { item in
Button(action: {
application.selectedApplication = item
}) {
VStack {
Image(item)
Text(item)
}
}
}
}
}
VStack(alignment: .center){
Text(application.selectedApplication)
}
}
}
}
编辑:
我已经更新了导航 link 和 @ObservedObject
但仍然无法正常工作:
这是更新后的 ProductList2 NavigationLink:
ForEach(matchedItems) { item in
NavigationLink(destination: ProductTabView(product: item, application: selectedOption.selectedApplication)){
ProductListRow(product: item)
}
}
这是 ProductTab5View 上的 @ObservedObject
,我也无法进行预览:
@ObservedObject private var application: selectedApplication
struct ProductTab5View_Previews: PreviewProvider {
static var previews: some View {
ProductTab5View(product: productData[0], application: application)
}
}
您正在使用 selectedApplication
的两个 不同 实例:
struct ProductList2: View {
@ObservedObject var selectedOption = selectedApplication()
struct ProductTab5View: View {
...
@ObservedObject private var application = selectedApplication()
您需要在两个视图中使用相同的实例。
struct ProductTab5View: View {
...
@ObservedObject var application: selectedApplication // declare only
// pass the already created instance to the child view
NavigationLink(destination: ProductTabView(product: item, application: selectedOption))
我正在尝试在 this article 之后使用 @Published
和 @ObservedObject
在两个视图之间传递数据,但由于某些原因我无法传递数据, @Published
只是保持初始状态,而不是在单击按钮时更改它。
第一个视图 ProductList2
导航到一个 tabView,我试图将数据传递到其中一个 tabView。任何帮助将不胜感激。
这是我创建 ObservableObject
和 @Published
class selectedApplication: ObservableObject {
@Published var selectedApplication = "All"
}
struct ProductList2: View {
@ObservedObject var selectedOption = selectedApplication()
var products: [ProductModel] = productData
var body: some View {
VStack {
HStack {
List{
ForEach(applicationsArray, id: \.self) { item in
Button(action: {
self.selectedOption.selectedApplication = item
}) {
HStack(){
Image(item)
Text(item)
}
}
}
}
}
List{
let matchedItems = products.filter {
product in
let list = product.application
for item in list {
if item == selectedOption.selectedApplication {
return true
}
}
return false
}
ForEach(matchedItems) { item in
NavigationLink(destination: ProductTabView(product: item)) {
ProductListRow(product: item)
}
}
}
}
}
}
这是我尝试检索数据的选项卡视图:
struct ProductTab5View: View {
var product: ProductModel
@ObservedObject private var application = selectedApplication()
var body: some View {
VStack(alignment: .leading){
Text(product.detailTabNames[3])
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 0){
ForEach(product.application, id: \.self) { item in
Button(action: {
application.selectedApplication = item
}) {
VStack {
Image(item)
Text(item)
}
}
}
}
}
VStack(alignment: .center){
Text(application.selectedApplication)
}
}
}
}
编辑:
我已经更新了导航 link 和 @ObservedObject
但仍然无法正常工作:
这是更新后的 ProductList2 NavigationLink:
ForEach(matchedItems) { item in
NavigationLink(destination: ProductTabView(product: item, application: selectedOption.selectedApplication)){
ProductListRow(product: item)
}
}
这是 ProductTab5View 上的 @ObservedObject
,我也无法进行预览:
@ObservedObject private var application: selectedApplication
struct ProductTab5View_Previews: PreviewProvider {
static var previews: some View {
ProductTab5View(product: productData[0], application: application)
}
}
您正在使用 selectedApplication
的两个 不同 实例:
struct ProductList2: View {
@ObservedObject var selectedOption = selectedApplication()
struct ProductTab5View: View {
...
@ObservedObject private var application = selectedApplication()
您需要在两个视图中使用相同的实例。
struct ProductTab5View: View {
...
@ObservedObject var application: selectedApplication // declare only
// pass the already created instance to the child view
NavigationLink(destination: ProductTabView(product: item, application: selectedOption))