从子视图返回视图后,NavigationLink 不可点击

NavigationLink is not clickable after returning to the view from it's child view

我的应用有一个多级布局 AView -> BView -> CView -> DView.

我变

window.rootViewController

到 BView 以“弹出”2 个顶部视图,但由于某种原因,当我回到 BView 时,它的 NavigationLink 不可点击。

关于如何解决这个问题有什么想法吗?好像BView不知道它变得可见了..

AView.swift:

import SwiftUI

struct AView: View {
    
    init() {
        print("AView init")
    }
    

    var body: some View {
        
        NavigationView {
            VStack {
                NavigationLink(destination: BView()) {
                    Text("This is View A, now go to View B.")
                }
            }
        }
    }
}

struct BView: View {
    init() {
        print("BView init")
    }
    

    var body: some View {
        NavigationLink(destination: CView()) {
                Text("This is View B, now go to View C.")
        }
    }
}

struct CView: View {
    
    init() {
        print("CView init")
    }
    
    var body: some View {
        NavigationLink(destination: DView()) {
                Text("This is View C, now go to View D.")
        }
    }
}

struct DView: View {
    init() {
        print("DView init")
    }
    
    var body: some View {

        Button(action: {
            print("button pressed")
            (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.toBView()

        },
               label: {
            Text("Back!")
        })

    }
}

SceneDelegate.swift:

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Create the SwiftUI view that provides the window contents.
        let aView =     AView()
        
        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: aView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    
    func toBView() {
        let bView = BView()
        window?.rootViewController = UIHostingController(rootView: bView)
    }

}

Asperi 已经说明了问题。您将 BView 设置为根级别。其实改一下应该问题不大。

但回到您的问题,为什么 NavigationLink 不再起作用。那是因为包含 NavigationView 的 ViewA 不再位于 RootLevel 中。因此,您将需要提供一个新的 NavigationView,但仅当 BView 是根视图时才需要。

所以在 BView 中,添加一个参数 isRootView,当您从 SceneDelegate

调用它时,您只会将其设置为 true
struct BView: View {
    init(isRootView: Bool = false) {
        print("BView init")
        self.isRootView = isRootView
    }
    
    var isRootView : Bool = false

    var body: some View {
        if isRootView {
            NavigationView {
                NavigationLink(destination: CView()) {
                        Text("This is View B, now go to View C.")
                }
            }
        }
        else
        {
            NavigationLink(destination: CView()) {
                    Text("This is View B, now go to View C.")
            }

        }
    }
}

这里是 SceneDelegate 的调用

func toBView() {
    let bView = BView(isRootView: true)
    window?.rootViewController = UIHostingController(rootView: bView)
}