当您导航并打开确认对话框时。当您选择是、否或取消时,应用程序所在的页面将被取消,它会将您带回上一页的表单。
我们还发现,警报也会发生这种情况。
这是一个足够简单的应用程序结构,顶层标签,然后是一个链接到子页面的菜单。
以下是该漏洞的快速演示:
我们制作了一个示例应用程序来演示这一点。我们如何在保持应用程序结构的同时防止这种情况发生?
import SwiftUI
@main
struct testApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
TabView() {
Form {
NavigationLink(destination: SubPage()) {
Image(systemName: "clock")
Text("Sub Page")
}
// This is where more menu options would be
}
.tag(1)
.tabItem {
Image(systemName: "square.grid.2x2")
Text("Tab 1")
}
// This is where more tab pages would be
}
}
}
}
}
struct SubPage: View {
@State private var confirmDialogVisible = false
var body: some View {
VStack {
Button{
confirmDialogVisible = true
} label: {
Text("popup")
}
}
.confirmationDialog("Confirm?", isPresented: $confirmDialogVisible) {
Button("Yes") {
print("yes")
}
Button("No", role: .destructive) {
print("no")
}
}
}
}
我们使用的是XCode 14.1,在iOS 16.1上运行
2条答案
按热度按时间nom7f22z1#
我通常使用
ViewModifer
来保持选项卡之间的一致性。一个修饰符用于选项卡的根,一个修饰符用于子项。然后视图使用它,就像这样。
我坚持使用
NavigationView
,因为这就是您的代码中的内容,但是如果我们考虑到新的NavigationStack
,这两个修饰符的可能性就会成倍增加。您可以包括仅在路径不为空、从任何位置返回到根目录等情况下才显示的自定义后退按钮。
苹果公司表示,
选项卡栏使用栏项目在同一视图中内容的互斥窗格之间导航
确保当用户导航到应用程序中的不同区域时,选项卡栏可见
https://developer.apple.com/design/human-interface-guidelines/components/navigation-and-search/tab-bars/
将
NavigationView
或NavigationStack
放在顶部是违反这些准则的,因此是无休止错误的来源,特别是当你考虑到iPadOS的时候。c6ubokkw2#
简单的解决方案是使用
navigationDestination
。我测试了一下,它再也不会自己弹出来了。