gpt4 book ai didi

ios - SwiftUI:onDismiss 没有被调用

转载 作者:行者123 更新时间:2023-11-29 05:26:36 26 4
gpt4 key购买 nike

我的问题是,如果我以编程方式关闭 View ,则不会调用 .sheet 中的 onDismiss 。如果我通常通过向下滑动来关闭 View onDismiss 则会正常调用。

View 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)
}

View 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")
}

最佳答案

@escaping 闭包添加到您的 PostTweetView 中,并将您的 onDismiss 代码添加到该闭包中。确保 PostTweetView 中的 init 也分配了所有局部变量:

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()
}
}

关于ios - SwiftUI:onDismiss 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58106317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com