Does anyone know how I can go to another view from an actionsheet in Swiftui?
Currently I use this as a button in actionsheet:
.actionSheet(isPresented: $actionsheet) {
ActionSheet(title: Text("Actions"), message: Text("Choose action"), buttons: [
.default(
NavigationLink(destination: adddetails()) {
Text("Add details")
}
),
.default(Text("New")),
.default(Text("Delete")),
.cancel()
])
}
But it won't build. Even though Xcode doesn't give me an error. Does anyone know what I can do?
You can control a
NavigationLinkprogrammatically by using theisActiveparameter with a binding. Then, in yourActionSheet, you can toggle that binding.The other key is that the
NavigationLinkneeds to be embedded in your original view hierarchy and not theActionSheet. You can conditionally display it with anifstatement so that it's only there if active (and thus invisible unless the navigate button is pressed):