Skip to content

Instantly share code, notes, and snippets.

@yutailang0119
Created January 25, 2020 07:54
Show Gist options
  • Save yutailang0119/716f5caf4789d0f1fca1f9b116a4d13f to your computer and use it in GitHub Desktop.
Save yutailang0119/716f5caf4789d0f1fca1f9b116a4d13f to your computer and use it in GitHub Desktop.
SwiftUI.View.popover(isPresented: Binding<Bool>) doesn't crashed, but SwiftUI.View.popover(item: Binding<Identifiable?>) crashes on dismiss Popover.
import SwiftUI
/*
This works fine.
*/
struct NonCrashView: View {
@State private var isPopup: Bool = false
var body: some View {
Button("Foo") {
self.isPopup.toggle()
}
.popover(isPresented: $isPopup) {
Button("Dismiss") {
self.isPopup = false
}
}
}
}
/*
This crashes on dismiss Popover.
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<UIPopoverPresentationController: xxxxxxxxxxxxxx>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*/
struct CrashView: View {
@State private var popover: Popover?
var body: some View {
Button("Bar") {
self.popover = .bar
}
.popover(item: $popover) { _ in
Button("Dismiss") {
self.popover = nil
}
}
}
}
extension CrashView {
enum Popover: Hashable, Identifiable {
case bar
var id: Self {
self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment