我的问题是,如果我以编程方式关闭视图,则不会调用.sheet
中的onDismiss
。如果我通常通过向下滑动来关闭视图onDismiss
则正常调用。
视图 1:
HStack {
Text("Twittere deine Antwort")
Spacer()
}
.onTapGesture {
self.showPostCommentView = true
}
.sheet(isPresented: self.$showPostCommentView, onDismiss: {
self.optionManager.getTweet() // <--- This does not get called
}) {
PostTweetView(postCommentTweet: self.optionManager.tweet, postTweetImage: self.image, profileImage: self.imageLoader.image)
}
视图 2:
@Environment(.presentationMode) private var presentationMode
Button(action: {
// some async operations... When finished completion gets called
self.tweetManager.sendTweet(completion: { self.presentationMode.wrappedValue.dismiss() })
}) {
Text("Twittern")
}
向PostTweetView
添加一个@escaping
闭包,并将onDismiss
代码添加到闭包中。确保 init inPostTweetView
也分配了所有局部变量:
PostTweetView
:
@Environment(.presentationMode) private var presentationMode
var onDismiss: () -> Void
init(/* All the other local variables in this view */ onDismiss: @escaping () -> Void) {
// initialize all the other local variables
self.onDismiss = onDismiss
}
Button(action: {
// some async operations... When finished completion gets called
self.tweetManager.sendTweet(completion: {
self.presentationMode.wrappedValue.dismiss()
self.onDismiss()
})
}) {
Text("Twittern")
}
view1
您最初的问题:
HStack {
Text("Twittere deine Antwort")
Spacer()
}
.onTapGesture {
self.showPostCommentView = true
}
.sheet(isPresented: self.$showPostCommentView, onDismiss: {
self.optionManager.getTweet() // <--- This does not get called
}) {
PostTweetView(postCommentTweet: self.optionManager.tweet, postTweetImage: self.image, profileImage: self.imageLoader.image) {
self.optionManager.getTweet()
}
}