【问题标题】:SwiftUI Navigationlink or Presentation Link not workingSwiftUI Navigationlink 或 Presentation Link 不起作用
【发布时间】:2019-11-14 14:20:13
【问题描述】:

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

【问题讨论】:

  • 您遗漏了几个可能对我们有所帮助的关键详细信息。 Xcode 的 beta 版本是什么?目标操作系统怎么样?也许更多的代码,以便我们可以复制您的问题?最后,究竟你想做什么?我通常希望 struct 成为目的地 - 但当然,我猜 Text 可以 工作 - 但是 CategoryItem 呢?还是name?甚至items?我的想法是……删掉你的大部分代码并让 something 工作。看起来您也在尝试...使用List(?) 并从中导航到详细视图?从您发布的内容来看,这真的很难说......
  • @mohitkejriwal cmets,我没有足够的声望点来发表评论,但请查看this ANSWER,展示使用NavigationLink 的清晰方法。谢谢,希望对某人有所帮助。

标签: swiftui


【解决方案1】:

我相信这是当前 SwiftUI 测试版中 PresentationLink 中的错误。在关闭模式后尝试重新打开模式时,我遇到了同样的错误。

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

EDIT2:PresentationLink 只有在嵌入到 NavigationBarItems 或 Lists 等内容中时才会出现问题。

【讨论】:

    【解决方案2】:

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

    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 中得到解决。

    【讨论】:

      【解决方案3】:

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

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

      我还添加了对 .isModalInPresentation(_ value: Bool) 修饰符的支持,以设置 UIViewController 的 isModalInPresentation 属性。希望 Apple 会尽快添加此功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-15
        • 2021-12-19
        • 2019-11-15
        • 1970-01-01
        • 2021-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多