我正在尝试从模态内部关闭模态。问题是,我想从NavigationBarItem
打开Modal。 Ive提供了一些演示代码,以及在某些情况下可以工作的示例和
案例01
1. Openning from NavigationBarItem
2. Closing by pressing Button in Modal
3. Openning again from Button -> WORKS
案例02
1. Openning from NavigationBarItem
2. Closing by swiping down
3. Openning again from NavigationBarItem -> WORKS
案例03
1. Openning from Button
2. Closing from Button
3. Openning again from NavigationBarItem -> DOESNT WORK
->问题似乎是当我按下按钮关闭时,模态关闭,但在后台仍被标记为活动状态。之后再按NavigationBarButton时,我在控制台中收到此错误:
Warning: Attempt to present <_TtGC7SwiftUIP13$7fff2c684d1c22SheetHostingControllerVS_7AnyView_: 0x7f9a6c52b3c0> on <UINavigationController: 0x7f9a6d85c200> which is already presenting (null)
这是代码:
struct TestView: View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
Button(action: {
self.showModal.toggle()
}) {
Text("Show Modal")
}.sheet(isPresented: $showModal) {
ModalView()
}
.navigationBarItems(trailing:
Button(action: {
self.showModal.toggle()
}) {
Text("Show Modal")
}.sheet(isPresented: $showModal) {
ModalView()
}
)
}
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentationMode
func dismiss() {
self.presentationMode.wrappedValue.dismiss()
}
var body: some View {
Button(action: {
self.dismiss()
}) {
Text("Save").bold()
}
}
}
答案 0 :(得分:0)
遇到此问题,我做了以下事情:
struct TestView: View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
Button(action: {
self.showModal.toggle()
}) {
Text("Show Modal")
}
.navigationBarItems(trailing:
Button(action: {
self.showModal.toggle()
}) {
Text("Show Modal")
}
)
}.sheet(isPresented: $showModal) {
ModalView()
}
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentationMode
func dismiss() {
self.presentationMode.wrappedValue.dismiss()
}
var body: some View {
Button(action: {
self.dismiss()
}) {
Text("Save").bold()
}
}
}
我希望这会有所帮助!