title: SwiftUI How to Create a button that adds an item to a list that contains a navigation link
body: <p>I've never done a post on this before, so hopefully this is set up correctly. </p>
  
  <p>I'm new to swift, and I want to create a button that adds a new item to a list that contains a navigation link to a file that it would create with linked data from the previous item, and I haven't found any way to do this after spending a few days on research and testing.</p>
  
  <p>This is what my app currently looks like for the layout I want in the end: <a href="https://gyazo.com/57250ce4ec7b6e13975c31454e2dfdff" rel="nofollow noreferrer">Q1</a> , and here is a preview of the different Q1-4 Views I mentioned: <a href="https://gyazo.com/dd2a11953d0fe2db549695ff64d63512" rel="nofollow noreferrer">Q1-4</a></p>
  
  <p>I know it's a lot, so let me explain more in depth: I want to have a list contained in what is called 'Q1' (as seen above) that starts out with 'Week 1', and as you click the add button, I want it to add a 'Week 2' and so forth up to 10 weeks. Once you hit 10 weeks, I want the user to have to change to the different view, 'Q2', which then they can add Week 11-20, and so forth until Q4, which limits it to a total of 40 Weeks. I want each week to contain a navigation link to a new view; however, I also want data from the previous week to be carried over as soon as I create the new week, so the user won't have to manually put in the previous week's data. </p>
  
  <p>I know how to do some of this by using a JSON file for the numbers, as I've seen tutorials on that, however, I don't see a point to this, as the only data I need for the Week numbers are 1-40, but I can't seem to get it to work with an array or anything. I do know that I can use an @EnvironmentObject to get the data I need from the other pages, but I'm not exactly sure how to set that up either. Other than that, I'm stuck! Here is my code:</p>
  
  <pre><code>import SwiftUI
  
  struct BillsView: View {
  
      @State private var quarterNumber = 0
  
      let quarterNumbers = [1, 2, 3, 4]
  
      var body: some View {
          NavigationView{
              VStack {
                 Section {
                      Picker("Quarter Number", selection: $quarterNumber) {
                          ForEach(0 ..&lt; quarterNumbers.count) {
                              Text("Q\(self.quarterNumbers[$0])")
                          }
                      }
                      .pickerStyle(SegmentedPickerStyle())
                      .padding(.horizontal)
                      if quarterNumber == 0 {
                          Q1View()
                      } else if quarterNumber == 1 {
                          Q2View()
                      } else if quarterNumber == 2 {
                          Q3View()
                      } else if quarterNumber == 3 {
                          Q4View()
                      }
                  }
              Spacer()
              }
  
              .navigationBarTitle("Bills")
              .navigationBarItems(leading: EditButton(),
              trailing: Button(action: {
                  //Adds the new week
              }){
                  Image(systemName: "plus.circle.fill")
              })
  
          }
      }
  }
  
  struct Q1View: View {
  
      @State private var weekNumber = 0
  
      let weekNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  
      var body: some View {
          List {
              NavigationLink(destination: Week1View()) {
                  Text("Week 1")
              }
          }
      }
  }
  
  struct Week1View: View {
      var body: some View {
          List {
              link(label: "Gross Income", destination: GrossIncome())
              link(label: "Expenses", destination: Expenses())
          }.navigationBarTitle(Text("Week 1"), displayMode: .inline)
      }
  
      private func link&lt;Destination: View&gt;(label: String, destination: Destination) -&gt; some   View {
          return NavigationLink(destination: destination) {
                  Text(label)
          }
      }
  }
  </code></pre>