I've been playing around with SwiftUI and got stumped on this simple thing. Basically, I'm trying to trigger a modal after tapping on an ActionSheet.Button
. Here's my code so far:
struct SomePage: View {
@State var showSheet = false
var body: some View {
Button(action: {
self.showSheet = true
}) {
Text("Show ation sheet")
}.presentation(sheet)
}
private var sheet: ActionSheet? {
let button = ActionSheet.Button.default(Text("Button") {
self.showSheet = false
// what now??
}
let action = ActionSheet(title: Text("Title"),
message: nil,
buttons: [button])
return showSheet ? action : nil
}
// This is the modal I'm trying to present
// after tapping on the action sheet button
private var modal: Modal {
return Modal(SomePage())
}
}
I've tried adding a second presentation
handler to the button and toggling a showModal
property but obviously the debugger complained about attempting a second modal presentation while the first one was still being presented.
Does anybody have an idea on how to make this work?
You're not far off from it.
Add another @State
to handle the presentation of the modal.
struct ContentView: View {
@State var showSheet = false
@State var showModal = false
var body: some View {
Button(action: {
self.showSheet = true
}) {
Text("Show action sheet")
}
.actionSheet(isPresented: $showSheet, content: actionSheet)
.sheet(isPresented: $showModal, content: { Text("Modal") })
}
private func actionSheet() -> ActionSheet {
let button = ActionSheet.Button.default(Text("Show modal")) {
self.showSheet = false
self.showModal = true
}
let actionSheet = ActionSheet(title: Text("Action Sheet"),
message: nil,
buttons: [button])
return actionSheet
}
}
Result :
Updated for Xcode 11 beta 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.