如何导航出 ActionSheet?
How to navigate out of a ActionSheet?
如何从我只能通过 Text
而不能通过 NavigationLink
的 ActionSheet
导航出来?
示例代码:
struct DemoActionSheetNavi: View {
@State private var showingSheet = false
var body: some View {
NavigationView {
Text("Test")
.actionSheet(isPresented: $showingSheet) {
ActionSheet(
title: Text("What do you want to do?"),
message: Text("There's only one choice..."),
buttons: [
.default(Text("How to navigate from here to HelpView???")),
])
}
}
}
}
你需要这样的东西:
struct DemoActionSheetNavi: View {
@State private var showingSheet = false
@State private var showingHelp = false
var body: some View {
NavigationView {
VStack {
Text("Test")
Button("Tap me") { self.showingSheet = true }
NavigationLink(destination: HelpView(isShowing: $showingHelp),
isActive: $showingHelp) {
EmptyView()
}
}
}
.actionSheet(isPresented: $showingSheet) {
ActionSheet(
title: Text("What do you want to do?"),
message: Text("There's only one choice..."),
buttons: [.cancel(),
.default(Text("Go to help")) {
self.showingSheet = false
self.showingHelp = true
}])
}
}
}
您有另一种状态,它以编程方式触发 NavigationLink
(您也可以使用 .sheet
和模态表示来实现)。您还需要将 showingHelp 作为 @Binding
传递以帮助视图能够重置它。
struct HelpView: View {
@Binding var isShowing: Bool
var body: some View {
Text("Help view")
.onDisappear() { self.isShowing = false }
}
}
如何从我只能通过 Text
而不能通过 NavigationLink
的 ActionSheet
导航出来?
示例代码:
struct DemoActionSheetNavi: View {
@State private var showingSheet = false
var body: some View {
NavigationView {
Text("Test")
.actionSheet(isPresented: $showingSheet) {
ActionSheet(
title: Text("What do you want to do?"),
message: Text("There's only one choice..."),
buttons: [
.default(Text("How to navigate from here to HelpView???")),
])
}
}
}
}
你需要这样的东西:
struct DemoActionSheetNavi: View {
@State private var showingSheet = false
@State private var showingHelp = false
var body: some View {
NavigationView {
VStack {
Text("Test")
Button("Tap me") { self.showingSheet = true }
NavigationLink(destination: HelpView(isShowing: $showingHelp),
isActive: $showingHelp) {
EmptyView()
}
}
}
.actionSheet(isPresented: $showingSheet) {
ActionSheet(
title: Text("What do you want to do?"),
message: Text("There's only one choice..."),
buttons: [.cancel(),
.default(Text("Go to help")) {
self.showingSheet = false
self.showingHelp = true
}])
}
}
}
您有另一种状态,它以编程方式触发 NavigationLink
(您也可以使用 .sheet
和模态表示来实现)。您还需要将 showingHelp 作为 @Binding
传递以帮助视图能够重置它。
struct HelpView: View {
@Binding var isShowing: Bool
var body: some View {
Text("Help view")
.onDisappear() { self.isShowing = false }
}
}