繁体   English   中英

SwiftUI Navigationlink或Presentation Link无法正常工作

[英]SwiftUI Navigationlink or Presentation Link not working

在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]接收回复失败,错误“操作已取消”

我相信这是当前SwiftUI beta中的PresentationLink中的错误。 我在解雇它时尝试重新打开模态时遇到同样的错误。

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

EDIT2:在链接到NavigationBarItems或Lists等内容时,PresentationLink似乎只是有缺陷。

这似乎是一个错误。 我设法掀起了一个(肮脏的)解决方法:

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中被打破。

编辑:我已通过新的反馈助手应用程序FB6525020提交了雷达。 请提高你自己和我的参考,希望这将由beta 4解决。

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

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

我还添加了对.isModalInPresentation(_ value:Bool)修饰符的支持,以设置UIViewController的isModalInPresentation属性。 希望Apple能过早添加这个。

暂无
暂无

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

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