Created
January 25, 2020 07:54
-
-
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.
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 | |
/* | |
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