SwiftUI NavigationLink或PresentationLink不起作用。

5
使用NavigationLink或PresentationLink时,在swiftUI中,导航控制器无法推送或呈现新的视图,会出现错误:"[WindowServer] display_timer_callback: unexpected state"。
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] 接收回复失败,错误为“操作被取消”。

1
你缺少几个关键细节,这些细节可能会帮助我们。Xcode的哪个beta版本?目标操作系统呢?也许还需要一些代码,以便我们可以复制您的问题?最后,您到底想做什么?我通常期望struct是目标 - 但当然,我猜Text也可以工作 - 但是CategoryItem呢?或者name?甚至是items?我的想法是...削减大部分代码并使其正常工作。看起来你正在尝试使用一个List(?)从中导航到详细视图?根据您发布的内容,很难说.... - user7014451
@mohitkejriwal 评论道,我没有足够的声望点来发表评论,但请查看此答案,演示了使用NavigationLink的清晰方式。谢谢,希望能帮到某些人。 - dbc
3个回答

3

我认为这是当前SwiftUI beta版中PresentationLink的错误。尝试在关闭模态窗口后重新打开它时,我得到了相同的错误。

编辑1: NavigationLink需要嵌入到NavigationView中,如果没有,则会显示消息[WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f)

编辑2:只有当嵌入在NavigationBarItems或列表等组件中时,PresentationLink才出现错误。


3

看起来是一个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中得到解决。


1

我已经创建了一个更加可靠的PresentationLink替代品。希望在beta 4发布后,它将不再需要。

你可以在这里找到一个要点: https://gist.github.com/petercv/3fba967a69b262901053fc8638b7851b

我还添加了对.isModalInPresentation(_ value: Bool)修饰符的支持,以设置UIViewController的isModalInPresentation属性。希望苹果很快也会添加这个功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接