在swiftUI中使用NavigationLink或演示文稿链接时,导航控制器不会推送或显示新的视图,会出现错误
“ [WindowServer] display_timer_callback:意外状态”
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]接收回复 失败,错误为“操作已取消”
答案 0 :(得分:3)
这似乎是一个错误。我设法解决了一个(肮脏的)解决方法:
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所指出,PresentationLink
嵌入NavigationView
时似乎已损坏。令人震惊的是,从Xcode beta 3开始,它仍然被破坏。
编辑:我已经通过新的反馈助手应用FB6525020提交了雷达报告。请举起您自己的参考地雷,希望它会在Beta 4中得到解决。
答案 1 :(得分:2)
我相信这是当前SwiftUI beta中PresentationLink中的错误。在关闭它后尝试重新打开模式时,我遇到了相同的错误。
EDIT1:
NavigationLink需要嵌入到NavigationView中,如果没有,则将显示消息[WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f)
EDIT2:PresentationLink似乎只是嵌入到NavigationBarItems或Lists之类的东西中。
答案 2 :(得分:1)
我创建了一个PresentationLink替代品,其工作原理更加可靠。希望Beta 4发布后将不再需要它。
您可以在此处找到要点: https://gist.github.com/petercv/3fba967a69b262901053fc8638b7851b
我还添加了对.isModalInPresentation(_ value:Bool)修饰符的支持,以设置UIViewController的isModalInPresentation属性。希望苹果会过早添加它。