Swiftui 在“中显示来自选项卡的警报”;“更多”;溢出将我返回到上一个视图
我有一个简单的操作,在使用TabView的SwiftUI应用程序中显示警报。然而,每当我第一次从一个被推到“更多”溢出屏幕的选项卡显示警报时,它会立即将我带回到之前打开的任何“主选项卡”(非更多) 这只在我第一次显示警报时发生,之后它会按预期工作。但是,在所有后续警报(当其行为正常时)中,我在控制台中获得此输出Swiftui 在“中显示来自选项卡的警报”;“更多”;溢出将我返回到上一个视图,swiftui,swiftui-tabview,Swiftui,Swiftui Tabview,我有一个简单的操作,在使用TabView的SwiftUI应用程序中显示警报。然而,每当我第一次从一个被推到“更多”溢出屏幕的选项卡显示警报时,它会立即将我带回到之前打开的任何“主选项卡”(非更多) 这只在我第一次显示警报时发生,之后它会按预期工作。但是,在所有后续警报(当其行为正常时)中,我在控制台中获得此输出 2021-01-07 12:19:49.289546-0700 Test App[48718:25806605] setViewControllers:animated: called
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.
一种可能的解决方法是在单击按钮时保持选择:
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 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")))
}
}
}