Swiftui 如何在工作表关闭时激发OnUpper方法?

Swiftui 如何在工作表关闭时激发OnUpper方法?,swiftui,Swiftui,我目前正在使用SwiftUI开发一个应用程序 当从同一结构创建的工作表关闭时,我想使用onAppear方法 有没有办法做到这一点 代码如下: 斯威夫特酒店 import SwiftUI struct BaseView: View { @State var isSheet:Bool = false var body: some View { VStack{ VStack{ Text(&q

我目前正在使用SwiftUI开发一个应用程序

当从同一结构创建的
工作表关闭时,我想使用
onAppear
方法

有没有办法做到这一点


代码如下:

斯威夫特酒店

import SwiftUI

struct BaseView: View {
    
    @State var isSheet:Bool = false
    
    var body: some View {
        VStack{
            VStack{
                Text("BaseView")

                Button(action:{
                    isSheet = true
                }){
                    Text("SHEET")
                }
            }
            .onAppear(){
                print("onAppear fiered")
            }
            .sheet(isPresented: $isSheet){
                Sheet()
            }
        }
    }
}
斯威夫特

import SwiftUI

struct Sheet: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {

        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }) {
            Text("CLOSE")
        }
    }
}


Xcode:Version12.0.1

只需将所有内容从表上的
移动到单独的函数中,并在表上调用该函数,如

var body: some View {
    VStack{
        VStack{
            Text("BaseView")

            Button(action:{
                isSheet = true
            }){
                Text("SHEET")
            }
        }
        .onAppear(){
            foo()       // << here !!
        }
        .sheet(isPresented: $isSheet, onDismiss: {
            foo()       // << and here !!
        }){
            Sheet()
        }
    }
}

func foo() {
   print("do some here")
}
var主体:一些视图{
VStack{
VStack{
文本(“BaseView”)
按钮(操作:{
isSheet=true
}){
正文(“表格”)
}
}
.onAppear(){
foo()//