gpt4 book ai didi

swiftui - 如何导航出 ActionSheet?

转载 作者:行者123 更新时间:2023-12-03 16:37:55 25 4
gpt4 key购买 nike

如何导航出ActionSheet在那里我只能通过 Text但不是 NavigationLink ?

示例代码:

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 }

}

}

关于swiftui - 如何导航出 ActionSheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com