我在关闭工作表后试图显示警报。但是,当我更新工作表中的@Binding时,主页中的警报没有出现。我不知道现在是什么问题。有人可以向我解释吗?这是使用@Binding的问题吗?如果执行此操作的方法/逻辑错误,应该使用哪种方法?
struct ContentView: View {
@State private var showSheet = false
@State var showAlert = false
var body: some View {
VStack {
Text("sheet")
.onTapGesture(perform: {
self.showSheet.toggle()
})
}
.sheet(isPresented: $showSheet) {
AddView(showAlert: self.$showAlert, showSheet: self.$showSheet)
}
.alert(isPresented: $showAlert){
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}
struct AddView: View {
@Binding var showAlert: Bool
@Binding var showSheet: Bool
var body: some View {
Button("Dismiss") {
self.showSheet = false
self.showAlert = true
}
}
}
答案 0 :(得分:1)
Sheet具有不同的上下文,因此像在原始代码中那样设置的警报在此处不可见。
这里是修改后的代码,可以正常工作。经过Xcode 12.1 / iOS 14.1的测试
struct ContentView: View {
@State private var showSheet = false
@State var showAlert = false
var body: some View {
VStack {
Text("sheet")
.onTapGesture(perform: {
self.showSheet.toggle()
})
}
.sheet(isPresented: $showSheet, onDismiss: { self.showAlert = true }) {
AddView(showSheet: self.$showSheet)
}
.alert(isPresented: $showAlert){
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}
struct AddView: View {
@Binding var showSheet: Bool
var body: some View {
Button("Dismiss") {
self.showSheet = false
}
}
}
答案 1 :(得分:1)
真正的问题是您试图解散工作表并同时显示警报。在您的代码中,如果您向警报添加0.5秒的延迟,它将起作用:
struct AddView: View {
@Binding var showAlert: Bool
@Binding var showSheet: Bool
var body: some View {
Button("Dismiss") {
self.showSheet = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.showAlert = true
}
}
}
}
但是,这样写起来要干净得多:
struct ContentView: View {
@State private var showSheet = false
@State var showAlert = false
var body: some View {
VStack {
Button(action: {
self.showSheet.toggle()
}, label: {
Text("sheet")
})
.accentColor(.primary)
}
.sheet(isPresented: $showSheet, onDismiss: showAlertFunction, content: {
AddView()
})
.alert(isPresented: $showAlert){
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
func showAlertFunction() {
showAlert.toggle()
}
}
struct AddView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss") {
self.presentationMode.wrappedValue.dismiss()
}
}
}