[英]Cannot pass data between views and also update in second view - SwiftUI
I am trying to pass data between 2 views using @Published
and @ObservedObject
following this article , but for some reason I cannot pass the data through, the @Published
just keeps at the initial state that it has been given instead of changing when the button is clicked.我试图在这篇文章之后使用@Published
和@ObservedObject
在 2 个视图之间传递数据,但由于某种原因我无法传递数据, @Published
只是保持它已经给出的初始状态,而不是更改按钮时被点击。
The first view ProductList2
navigates to a tabView and I am trying to pass the data to one of the tabViews.第一个视图ProductList2
导航到一个 tabView,我试图将数据传递给其中一个 tabView。 Any help would be appreciated.任何帮助,将不胜感激。
Here is my view where I am creating the ObservableObject
and @Published
这是我创建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)
}
}
}
}
}
}
This is the tabview view where I am trying to retrieve the data:这是我尝试检索数据的 tabview 视图:
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)
}
}
}
}
EDIT:编辑:
I have updated the navigation link and @ObservedObject
but still cannot get it working:我已经更新了导航链接和@ObservedObject
但仍然无法正常工作:
This is the updated ProductList2 NavigationLink:这是更新后的 ProductList2 NavigationLink:
ForEach(matchedItems) { item in
NavigationLink(destination: ProductTabView(product: item, application: selectedOption.selectedApplication)){
ProductListRow(product: item)
}
}
And this is the @ObservedObject
on ProductTab5View, I also cannot get preview working:这是 ProductTab5View 上的@ObservedObject
,我也无法预览工作:
@ObservedObject private var application: selectedApplication
struct ProductTab5View_Previews: PreviewProvider {
static var previews: some View {
ProductTab5View(product: productData[0], application: application)
}
}
You're using two different instances of selectedApplication
:您正在使用selectedApplication
两个不同实例:
struct ProductList2: View {
@ObservedObject var selectedOption = selectedApplication()
struct ProductTab5View: View {
...
@ObservedObject private var application = selectedApplication()
You need to use the same instance in both views.您需要在两个视图中使用相同的实例。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.