使用 SwiftUI,我们在列表中安装了一个按钮。为什么当我点击按钮显示模态然后再次关闭时,模态消失了?
Using SwiftUI, we installed a Button in the List. Why does the modal disappear when I tap the button to display the modal and then close it again?
我现在正在学习使用Xcode11正式版为SwiftUI创建示例代码。
我写了一个简单的代码来显示和隐藏模态。
此代码将一个按钮添加到列表并显示一个模式。
然而奇怪的是,关闭后再次点击按钮时,模态不再出现。
是否有此原因或任何解决方案?
当列表中有按钮时发生,但如果您从代码中仅删除列表,则模态框可以显示任意次数。
这是导致错误的代码。
struct ContentView: View {
@State var show_modal = false
var body: some View {
List {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}
}
这是一个不会导致错误的代码。
struct ContentView: View {
@State var show_modal = false
var body: some View {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}
唯一的区别是有没有List。
ModalView代码如下。
struct ModalView: View {
// 1. Add the environment variable
@Environment(\.presentationMode) var presentationMode
var body: some View {
// 2. Embed Text in a VStack
VStack {
// 3. Add a button with the following action
Button(action: {
print("dismisses form")
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Dismiss")
}.padding(.bottom, 50)
Text("This is a modal")
}
}
}
设置断点时,每次都会调用print("Button Pushed"),但是不会调用.sheet的ModalView,自然不会调用ModalViewclass的body .
我认为问题是您的 .sheet
不在 List
本身上,而是在导致错误的代码中的 Button
上。
试试这个:
struct ContentView: View {
@State var show_modal = false
var body: some View {
List {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}
我现在正在学习使用Xcode11正式版为SwiftUI创建示例代码。 我写了一个简单的代码来显示和隐藏模态。 此代码将一个按钮添加到列表并显示一个模式。 然而奇怪的是,关闭后再次点击按钮时,模态不再出现。
是否有此原因或任何解决方案?
当列表中有按钮时发生,但如果您从代码中仅删除列表,则模态框可以显示任意次数。
这是导致错误的代码。
struct ContentView: View {
@State var show_modal = false
var body: some View {
List {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}
}
这是一个不会导致错误的代码。
struct ContentView: View {
@State var show_modal = false
var body: some View {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}
唯一的区别是有没有List。
ModalView代码如下。
struct ModalView: View {
// 1. Add the environment variable
@Environment(\.presentationMode) var presentationMode
var body: some View {
// 2. Embed Text in a VStack
VStack {
// 3. Add a button with the following action
Button(action: {
print("dismisses form")
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Dismiss")
}.padding(.bottom, 50)
Text("This is a modal")
}
}
}
设置断点时,每次都会调用print("Button Pushed"),但是不会调用.sheet的ModalView,自然不会调用ModalViewclass的body .
我认为问题是您的 .sheet
不在 List
本身上,而是在导致错误的代码中的 Button
上。
试试这个:
struct ContentView: View {
@State var show_modal = false
var body: some View {
List {
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Text("Show Modal")
}
}.sheet(isPresented: self.$show_modal, onDismiss: {
print("dismiss")
}) {
ModalView()
}
}
}