0

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.
   }
}
Travgalax
  • 81
  • 1
  • 7
  • Unfortunately, your example doesn't compile because there are many different types that you haven't included. I will say that the primary issue is that you've included `FeatureView2` in the *action* of your `Button` -- not in the `View` hierarchy itself. There *is also* a `FeatureView` in the view hierarchy, but you don't pass a title to it, so it will always have a blank title. In your example, it's not clear what `item` is in `FeatureView1` or what you want to happen when the button is pressed. Should it be a blank title first and then change to "Button Called" when an item is pressed? – jnpdx Dec 01 '21 at 04:15
  • use .id(refresh) with refresh a state variables say an Int. When you want to update the view to refresh, you change its id by increasing the value of refresh. – user3069232 Dec 01 '21 at 10:33

1 Answers1

1

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