swiftui - 在“更多”溢出中显示来自选项卡的警报让我回到上一个视图
问题描述
我有一个简单的操作,在使用 TabView 的 SwiftUI 应用程序中显示警报。但是,每当我第一次显示来自被推送到“更多”溢出屏幕的选项卡的警报时,它会立即将我带回到我之前所在的任何“主选项卡”(非更多)。
这仅在我第一次显示警报时发生,之后它按预期工作。但是,在所有后续警报(当它行为正确时)我在控制台中得到这个输出
2021-01-07 12:19:49.289546-0700 Test App[48718:25806605] setViewControllers:animated: called on <UIMoreNavigationController 0x7fcecf826c00> while an existing transition or presentation is occurring; the navigation stack will not be updated.
如果“按钮”选项卡是主要选项卡之一(不在“更多”屏幕上),则不会发生这种情况。
StoreKit 的应用内购买弹出窗口也会发生同样的行为,而不仅仅是我启动的警报。
我怎样才能解决这个问题?这是一个 TabView 错误吗?
运行 Xcode 12.3
在模拟器上复制:iPhone 11、iPhone 12、iPad Pro
复制步骤(使用下面的代码):
- 启动应用程序
- 转到更多 -> 按钮选项卡
- 点击“显示警报”
- 显示警报,但其后面的视图立即返回到选项卡一
- 重复相同的步骤,它不会再次发生(必须重新启动应用程序)
import SwiftUI
struct ContentView: View {
let textTabs = ["One","Two","Three","Four","Five","Six","Seven","Eight"]
var body: some View {
TabView {
ForEach(textTabs, id: \.self) { name in
Text(name).tabItem {
Image(systemName: "circle")
Text(name)
}
}
ButtonTab().tabItem {
Image(systemName: "face.smiling")
Text("Button")
}
}
}
}
struct ButtonTab: View {
@State var showAlert = false
var body : some View {
VStack {
Button("show alert") {
showAlert = true
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("hello"),
dismissButton: .default(Text("ok")))
}
}
}
解决方案
一种可能的解决方法是在按钮单击时保持选择:
struct ContentView: View {
let textTabs = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"]
@State private var selection = "Two" // store selection as a `@State`
var body: some View {
TabView(selection: $selection) {
ForEach(textTabs, id: \.self) { name in
Text(name)
.tabItem {
Image(systemName: "circle")
Text(name)
}
}
ButtonTab(selection: $selection)
.tabItem {
Image(systemName: "face.smiling")
Text("Button")
}
.tag("Button") // add tag for `selection`
}
}
}
struct ButtonTab: View {
@State private var showAlert = false
@Binding var selection: String // pass selection as a `@Binding`
var body: some View {
VStack {
Button("show alert") {
showAlert = true
selection = "Button" // (force) set `selection` here
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("hello"),
dismissButton: .default(Text("ok")))
}
}
}
推荐阅读
- camera - ONVIF 设备管理器如何获取摄像机的密码摘要?
- javascript - apollo-link-state:如何在 typeDefs 少状态链接上指定对象类型参数?
- c++ - 仅在 C++ 中验证字符串输入是否为字母和空格
- python - 在 Python 中使用 for 循环和正则表达式从不同的 DataFrame 重命名相似的列
- php - 这是什么数据,能不能转成JSON
- php - 让用户将视频从我的站点服务器上传到他们的 youtube 频道
- python - 如何将 Panda 'to_json' 输出添加到休息调用
- google-apps-script - 密码重置后如何重新授权 Google Apps 脚本?
- git - 分裂后大块不适用
- android - 选择编辑文本时全屏打开搜索建议片段