关闭弹出窗口

Close a popover

我成功地在我的应用程序中显示了一个弹出窗口。它可以通过向下滑动来关闭。效果很好。但是,建议还显示一个按钮来关闭弹出窗口。我试图将其添加到代码中,并在网站上提供了有关弹出窗口和弹出窗口的所有提示,但我失败了。有人可以帮我吗?必须是一个简单的命令,但对于 xcode/swift 中的初学者来说,它并不容易找到。

显示弹出框的代码和弹出框本身的代码:

...

      //The popover code in the ContentView that starts the popover
      Button(action: {
              presentPopup = true
            }, label: {
            Image(systemName: "questionmark.square")
            .foregroundColor(Color.gray)
                   })
            .padding(.leading)
            .popover(isPresented: $presentPopup, arrowEdge: .bottom) {
            Toelichting()
            .font(.footnote)
            }



//The code in the popover view:
 var body: some View {
    
    VStack {
        
        HStack {
        
        Text("Introduction")
            .font(.title2)
            .fontWeight(.bold)
            .multilineTextAlignment(.leading)
            .padding([.top, .leading])
        
        //Spacer ()
        
        // This should be the button to return to the main screen that ins't working
        //   Button  (action: {
        //      self.dismiss(animated: true, completion: nil)
        //    }, label: {
        //    Image(systemName: "xmark.circle")
        //    .foregroundColor(Color.gray)
        //    })
        //        .padding([.top, .trailing])
        }
        
         Divider()
            .padding(.horizontal)
            .frame(height: 3.0)
            .foregroundColor(Color.gray)

...

按钮操作应该是什么?感谢您的宝贵时间和帮助!

您需要在两个视图之间传递一个 Binding 值,而不是使用 dismiss() 关闭弹出窗口。

这样,Binding的值要么是true,由主视图触发,要么是false,在弹出框内触发。

这是您的代码:

struct TopView: View {
    @State private var presentPopup = true    // This controls the popover
    
    var body: some View {
        //The popover code in the ContentView that starts the popover
        Button {
            presentPopup = true
        } label: {
            Image(systemName: "questionmark.square")
                .foregroundColor(Color.gray)
        }
        .padding(.leading)
        .popover(isPresented: $presentPopup, arrowEdge: .bottom) {
            
            // You need to pass the Binding to the popover
            Toelichting(presentMe: $presentPopup)
                .font(.footnote)
        }
    }
}

struct Toelichting: View {
    
    @Binding var presentMe : Bool    // This is how you trigger the dismissal
    
    var body: some View {
        VStack {
            
            HStack {
            
            Text("Introduction")
                .font(.title2)
                .fontWeight(.bold)
                .multilineTextAlignment(.leading)
                .padding([.top, .leading])
            
            Spacer ()
            
                // This should be the button to return to the main screen that NOW IT'S FINALLY working
               Button  (action: {
                   
                   // Change the value of the Binding
                   presentMe.toggle()
                   
                }, label: {
                Image(systemName: "xmark.circle")
                .foregroundColor(Color.gray)
                })
                    .padding([.top, .trailing])
            }
            
             Divider()
                .padding(.horizontal)
                .frame(height: 3.0)
                .foregroundColor(Color.gray)
        }
    }
}