1

我的应用程序有一个多级布局 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)
    }

}
4

1 回答 1

2

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

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

所以在 BView 里面,添加一个参数isRootView,当你从你的SceneDelegate

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)
}
于 2020-12-14T00:01:36.017 回答