0

我以前从未在此发表过文章,因此希望设置正确。

我是 swift 新手,我想创建一个按钮,将新项目添加到列表中,该列表包含指向文件的导航链接,该文件将使用上一个项目的链接数据创建,但我还没有找到任何方法在花了几天的时间进行研究和测试后才这样做。

这是我的应用程序当前最终想要的布局的样子:Q1,这是我提到的不同 Q1-4 视图的预览:Q1-4

我知道这很多,所以让我更深入地解释一下:我想要一个包含在所谓的“Q1”(如上所示)中的列表,该列表以“第 1 周”开头,当您单击添加按钮时,我希望它添加一个“第 2 周”等等,最多 10 周。一旦达到 10 周,我希望用户必须更改为不同的视图,“Q2”,然后他们可以添加第 11-20 周,依此类推,直到第 4 季度,这将其限制为总共 40 周。我希望每周都包含一个指向新视图的导航链接;但是,我还希望在创建新一周后立即结转前一周的数据,这样用户就不必手动输入前一周的数据。

我知道如何通过对数字使用 JSON 文件来完成其中的一些操作,因为我已经看到了关于此的教程,但是,我没有看到这一点,因为我需要的周数字的唯一数据是 1 -40,但我似乎无法让它与数组或任何东西一起使用。我知道我可以使用@EnvironmentObject 从其他页面获取我需要的数据,但我也不完全确定如何设置它。除此之外,我被困住了!这是我的代码:

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 ..< 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<Destination: View>(label: String, destination: Destination) -> some   View {
        return NavigationLink(destination: destination) {
                Text(label)
        }
    }
}
4

1 回答 1

0

我不太确定我是否理解你的正确,但我做了一个非常简单的例子来回答你的标题问题

import SwiftUI

struct ContentView: View {

    @State private var list : [String] = ["Chris"]
    @State private var quarterNumber = 0

    var body: some View {
        Group () {
            Button(action: {
                self.list.append("whatever")
            }) {
                Text("tap me")
            }
            NavigationView{

                List(list, id: \.self) { item in

                    NavigationLink(
                        destination: View2(text: "hallo")
                            .navigationBarTitle(Text("Categories"), displayMode: .automatic)
                    ) {
                        Text("Categories")
                    }.isDetailLink(false) // damit der Doof nicht rechts das nächste Menu öffnet

                }
            }
        }
    }
}

struct View2: View {

    var text : String

    var body: some View {

        Text(text)
    }
}
于 2019-10-26T09:15:11.710 回答