I have a view FooView
in a navigation stack presented via a NavigationLink
.
And FooView
has a button that triggers a ActionSheet
.
The problem is: when the ActionSheet
is dismissed (either cancelled or selected option), its parent view, i.e. FooView
is also dismissed, i.e. pops up from the navigation stack.
Is there a way to prevent it doing that?
More importantly, why is this happening? What dismissed FooView
?
iOS version: iOS 15 on iPhone
XCode: 14.2
Here is a minimum reproducible sample code.
I noticed that if I moved NavigationView
from outside of TabView
into ContentView
, then the issue is gone. But then the Tabs will show up even in FooView
, hence I kept NavigationView
outside TabView
. (And why does that matter?)
import SwiftUI
@main
struct ViewDismissApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
TabView {
ContentView(title: "Tab 1")
.tabItem {
Label("Tab1", systemImage: "wifi")
}
ContentView(title: "Tab 2")
.tabItem {
Label("Tab2", systemImage: "wrench.adjustable")
}
}
}
.navigationViewStyle(.stack)
}
}
}
struct ContentView: View {
let title: String
var body: some View {
VStack {
NavigationLink {
FooView()
} label: {
Text(title)
}
}
}
}
struct FooView: View {
@State private var showActions = false
var body: some View {
VStack {
Text("Foo View")
actionButton()
}
}
private func actionButton() -> some View {
Button(action: {
showActions = true
}) {
Image(systemName: "arrow.up.circle")
}
.actionSheet(isPresented: $showActions) {
ActionSheet(title: Text("Actions"),
message: nil,
buttons: [
.default(Text("Option 1")) {
// TODO
},
.default(Text("Option 2")) {
// TODO
},
.cancel()
])
}
}
}
Thanks!