ForEach(self.items.identified(by: \.name)) { item in
NavigationLink(destination: Text("DT In the House")) {
CategoryItem(item: item)
}
}
[] nw_connection_receive_internal_block_invoke [C4] 接收回复失败,错误为“操作被取消”。
ForEach(self.items.identified(by: \.name)) { item in
NavigationLink(destination: Text("DT In the House")) {
CategoryItem(item: item)
}
}
我认为这是当前SwiftUI beta版中PresentationLink的错误。尝试在关闭模态窗口后重新打开它时,我得到了相同的错误。
编辑1:
NavigationLink需要嵌入到NavigationView中,如果没有,则会显示消息[WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f)
编辑2:只有当嵌入在NavigationBarItems或列表等组件中时,PresentationLink才出现错误。
看起来是一个bug。我已经设法想出了一个(糟糕的)解决方法:
private enum SetPresentedViewKey: EnvironmentKey {
static var defaultValue: (AnyView?) -> () {
fatalError()
}
}
private extension EnvironmentValues {
var setPresentedView: (AnyView?) -> () {
get {
self[SetPresentedViewKey.self]
} set {
self[SetPresentedViewKey.self] = newValue
}
}
}
/// A replacement for the buggy (as of Xcode 11 b3) `PresentationLink`.
public struct PresentationLink2<Destination: View, Label: View>: View {
public let destination: Destination
public let label: Label
@Environment(\.setPresentedView) private var setPresentedView
@State private var presentedView: AnyView? = nil
public init(destination: Destination, @ViewBuilder _ label: () -> Label) {
self.destination = destination
self.label = label()
}
private struct _Body<Destination: View, Label: View>: View {
@Environment(\.setPresentedView) private var setPresentedView
let destination: Destination
let label: Label
init(destination: Destination, label: Label) {
self.destination = destination
self.label = label
}
var body: some View {
Button(action: present, label: { label })
}
func present() {
setPresentedView(AnyView(destination))
}
}
public var body: some View {
_Body(destination: destination, label: label)
.environment(\.setPresentedView, { self.presentedView = $0 })
.presentation(presentedView.map {
Modal($0, onDismiss: { self.presentedView = nil })
})
}
}
只需将上面的代码复制到您的代码库中,然后使用PresentationLink2
代替PresentationLink
。
如@kozlowsqi所指出的那样,在NavigationView
中嵌入PresentationLink
似乎存在问题。令人担忧的是,即使在Xcode beta 3中仍然存在此问题。
编辑:我已通过新的Feedback Assistant应用程序提交了一个radar,编号为FB6525020。请提出自己的问题并引用我的反馈编号,希望这个问题能在beta 4中得到解决。
我已经创建了一个更加可靠的PresentationLink替代品。希望在beta 4发布后,它将不再需要。
你可以在这里找到一个要点: https://gist.github.com/petercv/3fba967a69b262901053fc8638b7851b
我还添加了对.isModalInPresentation(_ value: Bool)修饰符的支持,以设置UIViewController的isModalInPresentation属性。希望苹果很快也会添加这个功能。
struct
是目标 - 但当然,我猜Text
也可以工作 - 但是CategoryItem
呢?或者name
?甚至是items
?我的想法是...削减大部分代码并使其正常工作。看起来你正在尝试使用一个List
(?)从中导航到详细视图?根据您发布的内容,很难说.... - user7014451NavigationLink
的清晰方式。谢谢,希望能帮到某些人。 - dbc