开发者社区> 问答> 正文

在快速UI中从另一个视图中排除模态视图

我想在SwiftUI中取消一个模态视图,但我就是不能

这是我的代码:

内容视图:

import SwiftUI

enum Destination {
    case modal
}

struct ContentView: View {
    @Environment(\.presentationMode) var presentationMode

    @State private var showModal = false

    var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false) {
                VStack{
                    SubscribeButtonView(buttonTitle: "Modal", destination: .modal, showModal: $showModal)
                            .padding(.top, 50)
                            .padding(.leading, 30)
                            .padding(.trailing, 30)
                    Spacer()
                }
            }
            .navigationBarTitle(Text("Menu"))
        }
    }
}

ModalView:

import SwiftUI

struct ModalView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false) {
                    VStack(alignment: .leading) {
                     Spacer()
                        SaveButtonView(origin: .modal)
                        Spacer()
                    }
                    .padding(.leading, 20)
                    .padding(.trailing, 20)
            }
            .navigationBarTitle("Modal")
            .navigationBarItems(trailing: Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("Cancel")
            })
        }
    }
}

SubscribeButtonView:

import SwiftUI

struct SubscribeButtonView: View {
    @Environment(\.presentationMode) var presentationMode

    var buttonTitle: String
    var destination: Destination

    @Binding var showModal: Bool

    var body: some View {
        Button(action: {
            self.showModal.toggle()
        }) {
            HStack {
                Image(systemName: "plus.circle")
                    .font(.body)
                Text(buttonTitle)
            }
        }.sheet(isPresented: $showModal) {
            if self.destination == .modal {
                ModalView()
            }
        }
        .padding()
    }
}

SaveButtonView:

import SwiftUI

struct SaveButtonView: View {
    @Environment(\.presentationMode) var presentationMode
    var origin: Destination

    var body: some View {
        Button(action: {
            //THIS IS NOT WORKING
            self.presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Text("Save")
            }
        }
        .padding()
    }
}

我试着创建一个非常简单的新项目,只有一个状态来调用模式、菜单、模式和两个按钮,它工作得很好。我不明白为什么上面的代码不起作用

有人有同样的问题吗?

展开
收起
游客5akardh5cojhg 2019-12-05 16:44:52 414 0
1 条回答
写回答
取消 提交回答
  • 您应该将演示模式绑定到ModalView

    struct SaveButtonView: View {
                 //@Environment(\.presentationMode) var presentationMode
                @Binding var presentationMode : PresentationMode
                var origin: Destination
    
                var body: some View {
                    Button(action: {
                        //THIS IS NOT WORKING
                        self.$presentationMode.wrappedValue.dismiss()
                    }) {
                        HStack {
                            Text("Save")
                        }
                    }
                    .padding()
                }
            }
    
    
         struct ModalView: View {
                @Environment(\.presentationMode) var presentationMode
    
                var body: some View {
    
                    NavigationView {
                        ScrollView(.vertical, showsIndicators: false) {
                                VStack(alignment: .leading) {
                                 Spacer()
                                    SaveButtonView( presentationMode: self.presentationMode, origin: .modal)
                                    Spacer()
                                }
                                .padding(.leading, 20)
                                .padding(.trailing, 20)
                        }
                        .navigationBarTitle("Modal")
                        .navigationBarItems(trailing: Button(action: {
                            self.presentationMode.wrappedValue.dismiss()
                        }) {
                            Text("Cancel")
                        })
                    }
                }
            }
    
    2019-12-05 16:45:43
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用TensorFlow搭建智能开发系统自劢生成App UI代码 立即下载
Fusion Design - 企业级UI解决方案揭秘 立即下载
使用TensorFlow搭建智能开发系统自动生成App UI 立即下载