简体   繁体   English

SwiftUI Navigationlink或Presentation Link无法正常工作

[英]SwiftUI Navigationlink or Presentation Link not working

When using NavigationLink or presentation link in swiftUI, the navigation controller doesn't push or present a new View, getting error 在swiftUI中使用NavigationLink或演示文稿链接时,导航控制器不会推送或显示新视图,从而出现错误

"[WindowServer] display_timer_callback: unexpected state" “[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] Receive reply failed with error "Operation canceled" [] nw_connection_receive_internal_block_invoke [C4]接收回复失败,错误“操作已取消”

I believe this is bug in PresentationLink in current SwiftUI beta. 我相信这是当前SwiftUI beta中的PresentationLink中的错误。 I get the same error while trying to reopen modal after dismissing it. 我在解雇它时尝试重新打开模态时遇到同样的错误。

EDIT1: NavigationLink requires to be embedded in NavigationView and if there is none will present message [WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f) EDIT1:NavigationLink需要嵌入在NavigationView中,如果没有则会显示消息[WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f)

EDIT2: PresentationLink only appears to be buggy while embedded in things like NavigationBarItems or Lists etc. EDIT2:在链接到NavigationBarItems或Lists等内容时,PresentationLink似乎只是有缺陷。

It seems to be a bug. 这似乎是一个错误。 I've managed to whip up a (dirty) workaround: 我设法掀起了一个(肮脏的)解决方法:

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 })
            })
    }
}

Just copy the code above into your codebase and use PresentationLink2 instead of PresentationLink . 只需将上面的代码复制到您的代码库中,然后使用PresentationLink2而不是PresentationLink


As noted by @kozlowsqi, PresentationLink seems to be broken when embedded in a NavigationView . 正如@kozlowsqi所指出的,当在NavigationView嵌入时, PresentationLink似乎被打破了。 What's alarming is that it's still broken as of Xcode beta 3. 令人担忧的是,它仍然在Xcode beta 3中被打破。

Edit: I've filed a radar through the new Feedback Assistant app, FB6525020. 编辑:我已通过新的反馈助手应用程序FB6525020提交了雷达。 Please raise your own and reference mine, and hopefully this will be resolved by beta 4. 请提高你自己和我的参考,希望这将由beta 4解决。

I've created a PresentationLink replacement that works far more reliable. 我创建了一个更可靠的PresentationLink替代品。 Hopefully it won't be needed anymore as soon as beta 4 is released. 希望在beta 4发布后不再需要它。

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

I've also added support for a .isModalInPresentation(_ value: Bool) modifier to set the isModalInPresentation property of UIViewController. 我还添加了对.isModalInPresentation(_ value:Bool)修饰符的支持,以设置UIViewController的isModalInPresentation属性。 Hopefully Apple will add this too soon. 希望Apple能过早添加这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM