添加项目时如何更新列表?

时间:2019-12-18 23:14:06

标签: ios swift list swiftui

我正在尝试更新列表以反映添加新项目时的情况。目前,一旦按下“ paperplane”按钮,它就不会显示。如果您看到参考文献,我就省去了重复的代码块。

我怀疑问题出在MergedView的按钮中,但找不到解决方案。它可以编译,但不执行预期的操作。

import Foundation

class One: Identifiable, ObservableObject { // ObservableObject requires class

    let id: UUID
    var item: String = ""
    @Published var completed: Bool = false // this will affect the UI

    init(item: String, completed: Bool) {
        id = UUID()
        self.item = item
        self.completed = completed
    }

    }

    class OneList: ObservableObject {
        @Published var items = [One(item:"",completed:false)]
    }




    struct VitalRow: View {
        @EnvironmentObject var item: One
        var body: some View {
            HStack{
                Image(systemName: item.completed ? "checkmark.circle" : "circle")
                Text(item.item)
            }
        }
    }

    struct ButtonView: View {

    @EnvironmentObject var itemss1: OneList
    @EnvironmentObject var itemss2: TwoList
    @EnvironmentObject var itemss3: ThreeList
    @EnvironmentObject var itemss4: FourList

    @State private var  showingAdditem: Bool = false
    var body: some View {
        VStack{
            Spacer()

            HStack{
                Spacer()

                Button(action:{
                    self.showingAdditem = true
                }){
                    Image(systemName: "plus")
                        .font(Font.system(size: 25,weight: .regular))
                        .font(.largeTitle)
                        .frame( width: 50, height: 50)
                        .foregroundColor(.white)
                }.sheet(isPresented: $showingAdditem)
                {
                    MergedView()
                        .environmentObject(OneList())
                        .environmentObject(TwoList())
                        .environmentObject(ThreeList())
                        .environmentObject(FourList())
                    //items1: self.itemss1, items2: self.itemss2, items3: self.itemss3, items4: self.itemss4)
                }.background(Color.blue)
                    .cornerRadius(25)
                    .shadow(color: Color.black.opacity(0.3), radius: 3,
                            x: 3,
                            y: 3)
                    .padding()
                    .offset(x: -5,
                            y: 15)
            }
        }
    }
}


    struct MergedView: View {

    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var items1: OneList
    @EnvironmentObject var items2: TwoList
    @EnvironmentObject var items3: ThreeList
    @EnvironmentObject var items4: FourList

    /*@ObservedObject var items1: OneList
    @ObservedObject var items2: TwoList
    @ObservedObject var items3: ThreeList
    @ObservedObject var items4: FourList*/

    @State private var item =  ""

    @State var blockcolor1 = Color.red.opacity(0.90)
    @State var blockcolor2 = Color.red.opacity(0.70)
    @State var blockcolor3 = Color.red.opacity(0.45)
    @State var blockcolor4 = Color.red.opacity(0.20)

    @State var itemvalue: Int = 0

    var body: some View {
        GeometryReader { geo in
            ZStack{
                NavigationView{
                    HStack{
                        TextField("Item", text: self.$item)
                            .padding()
                        Button(action:{

                            if self.itemvalue == 1{
                                let things1 = One(item: self.item, completed: false)
                                self.items1.items.append(things1)
                                self.presentationMode.wrappedValue.dismiss()
                            }
                      {
                            Image(systemName:"paperplane")
                                .font(.headline)
                                .padding()
                        }
          HStack(spacing: 10) {
                                Rectangle()
                                    .fill(self.blockcolor1)
                                    //.fill(Color.red.opacity(0.90))
                                    //.frame(width: 140,height: 180)
                                    .frame(width: geo.size.width/3, height: geo.size.height/4.6)
                                    .cornerRadius(15)
                                    .shadow(radius: 6)
                                    .onTapGesture{
                                        self.blockcolor1 = Color.red.opacity(0.1)
                                        self.itemvalue = 1
                                }




    struct CheckboxList: View {

    @EnvironmentObject var item1List: OneList
    @EnvironmentObject var item2List: TwoList
    @EnvironmentObject var item3List: ThreeList
    @EnvironmentObject var item4List: FourList

    var body: some View {
        NavigationView{
            List {
                Section(header: Text("Vital")) {
                    ForEach(item1List.items.indices) { index in
                        VitalRow()
                            .environmentObject(self.item1List.items[index])
                            .onTapGesture {
                                self.item1List.items[index].completed.toggle()
                        }
                    }
                }
    }

谢谢。

0 个答案:

没有答案