Created
March 17, 2024 19:16
-
-
Save mac2000/12e90a891c1b800910685bd6f6c90c03 to your computer and use it in GitHub Desktop.
dismissable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct DismissableNavigationLinkDetailView: View { | |
@Environment(\.dismiss) var dismiss | |
@Binding var item: String? | |
let items = ["Item 1", "Item 2", "Item 3", "Item 4"] | |
var body: some View { | |
List(selection: $item) { | |
ForEach(items, id: \.self, content: Text.init) | |
} | |
.onChange(of: item) { oldValue, newValue in | |
guard let item = newValue else { return } | |
print("Selected: \(item)") | |
dismiss() | |
} | |
} | |
} | |
struct DismissableNavigationLink: View { | |
@State private var item: String? | |
var body: some View { | |
Form { | |
NavigationLink { | |
DismissableNavigationLinkDetailView(item: $item) | |
} label: { | |
HStack { | |
Text("Custom") | |
Spacer() | |
Text(item ?? "N/A") | |
} | |
} | |
} | |
} | |
} | |
#Preview { | |
NavigationStack { | |
DismissableNavigationLink() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment