简体   繁体   English

SwiftUI - 呈现 3 个具有不同参数的不同视图

[英]SwiftUI - Present 3 different Views with different parameter

I need to present 3 different Views.我需要呈现 3 个不同的视图。

   AddListView
   ChangeColor
   EditListView

They take different paramater.他们采用不同的参数。 AddListView does not have parameter while ChangeColor and EditListView takes Color and NSManagedObject respectively. AddListView没有参数,而ChangeColorEditListView分别采用ColorNSManagedObject However for the sake of simplicity, EditListView 's paramter is integer in this example.但是为了简单起见,在这个例子中, EditListView的参数是 integer 。

I am using .fullScreenCover(item: <#T##Binding<Identifiable?>#>, content: <#T##(Identifiable) -> View#>) for presenting them.我正在使用.fullScreenCover(item: <#T##Binding<Identifiable?>#>, content: <#T##(Identifiable) -> View#>)来展示它们。

.fullScreenCover(item: $presentedViewType) { type in
    if type == .AddListView {
        AddListView()
    }
    else if type == .EditListView {
        if let index = selectedIndex {
            EditListView(index: index)
        }
    }
    
    else if type == .ChangeColor {
        if let color = selectedColor {
            ColorView(color: color)
        }
    }
}

selectedIndex and selectedColor is nil even though I initialize them before initializing presentedViewType . selectedIndexselectedColornil ,即使我在初始化presentedViewType之前对其进行了初始化。 And hence, an EmptyView is presented.因此,出现了一个EmptyView

This is the project.这是项目。

enum PresentedViewType: Identifiable {
    case AddListView
    case ChangeColor
    case EditListView
    
    var id: Int {
        return hashValue
    }
}


struct ContentView: View {
    
    @State var presentedViewType: PresentedViewType?
    
    @State var selectedColor: Color?
    
    @State var selectedIndex: Int?
    
    var body: some View {
        NavigationView {
            List {
                
                Section {
                    NavigationLink(destination: Text("All")) {
                        Text("All")
                    }
                    .background(Color.blue)
                    .contextMenu {
                        Button(action: {
                            selectedColor = .blue
                            presentedViewType = .ChangeColor
                        }) {
                            Label("Change Color", systemImage: "paintbrush.pointed.fill")
                        }
                    }
                }
                
                ForEach(0..<10) { index in
                    NavigationLink(destination: Text("Row Details \(index)")) {
                        Text("Row \(index)")
                    }
                    .contextMenu {
                        Button(action: {
                            selectedIndex = index
                            presentedViewType = .EditListView
                        }) {
                            Label("Edit", systemImage: "square.and.pencil")
                        }
                    }
                }
                
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        presentedViewType = .AddListView
                    }) {
                        Label("Add", systemImage: "plus")
                    }
                }
            }
            .fullScreenCover(item: $presentedViewType) { type in
                if type == .AddListView {
                    AddListView()
                }
                else if type == .EditListView {
                    if let index = selectedIndex {
                        EditListView(index: index)
                    }
                }
                
                else if type == .ChangeColor {
                    if let color = selectedColor {
                        ColorView(color: color)
                    }
                }
            }
        }
    }
}



struct ColorView: View {
    @Environment(\.presentationMode) var presentationMode
    
    @State var color: Color
    
    var body: some View {
        NavigationView {
            Text("Color View")
                .background(color)
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button(action: {
                            presentationMode.wrappedValue.dismiss()
                        }) {
                            HStack {
                                Image(systemName: "xmark")
                            }
                        }
                    }
                }
        }
    }
}



struct  AddListView: View {
    @Environment(\.presentationMode) var presentationMode
    @State var text: String = ""
    
    var body: some View {
        NavigationView {
            TextField("", text: $text)
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button(action: {
                            presentationMode.wrappedValue.dismiss()
                        }) {
                            HStack {
                                Image(systemName: "xmark")
                            }
                        }
                    }
                }
        }
    }
}



struct  EditListView: View {
    @Environment(\.presentationMode) var presentationMode
    
    @State var index: Int
    
    var body: some View {
        NavigationView {
            Text("Row \(index)")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button(action: {
                            presentationMode.wrappedValue.dismiss()
                        }) {
                            HStack {
                                Image(systemName: "xmark")
                            }
                        }
                    }
                }
        }
    }
}

I have to mention that they do not have fixed value.我不得不提到它们没有固定的价值。 They have different value depending on which row you need to edit.它们具有不同的值,具体取决于您需要编辑的行。

How to pass selectedIndex and selectedColor to EditListView and ColorView respectively?如何将selectedIndexselectedColor分别传递给EditListViewColorView

Update更新

EditListView takes only selectedIndex while ColorView takes only selectedColor EditListView只需要selectedIndexColorView只需要selectedColor

You need to have @Binding properties inside EditListView and ColorView您需要在EditListViewColorView中有@Binding属性

struct EditListView: View {

    @Binding var selectedIndex: Int?

    // rest of view implementation
}

struct ColorView: View {

    @Binding var selectedIndex: Int?

    // rest of view implementation
}

and then pass the binding in the initialisers然后在初始化程序中传递绑定

.fullScreenCover(item: $presentedViewType) { type in
    if type == .AddListView {
        AddListView()
    } else if type == .EditListView {
        EditListView(index: $selectedIndex)
    } else if type == .ChangeColor {
        ColorView(color: $selectedColor)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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