簡體   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