简体   繁体   中英

SwiftUI pass data from view to view

I spent 14 hours trying to figure it out today, I went over here, googled, and watched videos but nothing helped. I gave up so I decided to ask a question.

Typically, I have two views in one, such as the list on the left and details on the right. On the iPhone, I was able to use the sheet to pop up the second view without any issues, but on the iPad, I have a second view on the right side and it does not update when I click on the list. I tried @Binging and @State, but it didn't work.

How can I pass data from one view to another?

The navigation link code:

let navigationItemList: [NavigationItems] = [NavigationItems(image: Image(systemName: "hourglass"), title: "Feature 1", subtitle: "Subtitle", linkView: AnyView(FeatureView1())),
                                             NavigationItems(image: Image(systemName: "clock.arrow.2.circlepath"), title: "Feature 2", subtitle: "Subtitle", linkView: AnyView(FeatureView2()))]
struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section(footer: MainFooterView()) {
                    ForEach(navigationItemList) { items in
                        HStack {
                            items.image?
                                .frame(width: 30, height: 30)
                                .font(.system(size: 25))
                                .foregroundColor(color3)
                            Text("")
                            NavigationLink(items.title, destination: items.linkView)
                        }
                        .listRowBackground(Color.clear)
                        .listRowSeparatorTint(.clear)
                        
                    }
                    .navigationTitle("Features")
                    .listStyle(.insetGrouped)
                }
            }
        }
    }
}

First view:

struct FeatureView1 : View {
var body: some View {
   HStack {
      List(item) { items in
         Button("Click to update title in Feature View 2") {
            FeatureView2(title: "Button Called") //Nothing happened
         }
      }
      FeatureView2()
   }
}

Second view:

var body: some View {
   var title = ""
   ZStack {
      Text(title) //When I click a button from the list, it should show "Button Called", but nothing happens.
   }
}

Before automating it, try to make a simple example like that to understand how to share datas between views: (As you can see, destination view take the title as parameter)

 struct ContentView: View { var body: some View { NavigationView { NavigationLink(destination: FeatureView1(title: "FeatureView1")) { Text("Go to FeatureView1") } } } } struct FeatureView1: View { var title: String var body: some View { Text(title) NavigationLink(destination: FeatureView2(title: "FeatureView2")) { Text("Go to FeatureView2") } } } struct FeatureView2: View { var title: String var body: some View { Text(title) } }

There are many other ways to share datas between views according to your use case, I let you see @EnvironmentObject, @Binding etc later

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM