SwiftUI:onDismiss不被调用



我的问题是,如果我以编程方式关闭视图,则不会调用.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()
}
}

相关内容

  • 没有找到相关文章

最新更新