SwiftUI:尝试使用NavigationView / NavigationLink

时间:2020-10-30 08:59:12

标签: routes swiftui uikit swiftui-navigationlink swiftui-navigationview

我正在尝试将UIKit应用移植到UISwift中。在这个应用程序中,我创建了一个通用路由器,其工作方式如下:

enum Route {
    case home
    case settings
    case specific(setting: Int)

    var viewController: UIViewController {
        switch self {
        case .home:
            return HomeVC {
                Router.to(.settings)
            }
        case .settings:
            return SettingsVC { specificSetting in
                Router.to(.specific(setting: specificSetting))
            }
        case .specific(let setting):
            return SpecificSettingVC(setting)
        }
    }
}

相关的viewControllers:

class HomeVC: UIViewController {
    var toSettings: () -> Void
    
    init(_ toSettings: @escaping () -> Void) {
        self.toSettings = toSettings
        super.init(nibName: "HomeVC", bundle: .main)
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
}

class SettingsVC: UIViewController {
    var toSpecificSetting: (Int) -> Void
    
    init(_ specificSetting: @escaping (Int) -> Void) {
        self.toSpecificSetting = specificSetting
        super.init(nibName: "SettingsVC", bundle: .main)
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
}

class SpecificSettingVC: UIViewController {
    private let settingIndex: Int
    
    init(_ index: Int) {
        self.settingIndex = index
        super.init(nibName: "SpecificSettingVC", bundle: .main)
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
}

路由器很简单:

struct Router {
    static func to(_ route: UIRoute) {
        // In this function I find the current navigationController 
        // and call .pushViewController(route.viewController, animated: true)
    }
}

我喜欢它,因为这个Route枚举有点像站点地图,可以快速向我显示应用程序中的所有路线。

由于this tutorial,我想在SwiftUI中做类似的事情:

路由枚举:

enum Route {
    case blue
    case red
    var view: some View {
        VStack {
            switch self {
            case .blue:
                BlueView()
            case .red:
                RedView()
            }
        }
    }
}

路由器:

class Router: ObservableObject {
    let objectWillChange = PassthroughSubject<Router, Never>()
    var currentRoute: Route = .blue {
        didSet {
            withAnimation {
                objectWillChange.send(self)
            }
        }
    }
}

路由器视图:

struct RouterView: View {
    @EnvironmentObject var router: Router
    
    var body: some View {
        VStack {
            switch router.currentRoute {
            case .blue:
                BlueView(toRed: {
                    router.currentRoute = .red
                })
            case .red:
                RedView(back: {
                    router.currentRoute = .blue
                })
            }
        }
    }
}

当然是应用程序:

@main
struct MainApp: App {
    var body: some Scene {
        WindowGroup {
            RouterView()
                .environmentObject(ViewRouter())
        }
    }
}

例如,BlueView:

struct BlueView: View {
    @EnvironmentObject var viewRouter: ViewRouter
    var toRed: () -> Void
    
    var body: some View {
        ZStack {
            Color.blue
            Button(action: {
                toRed()
            }, label: {
                Text("Tap me!")
                    .foregroundColor(.white)
            })
        }
    }
}

尽管可以正常工作,但这仅替换了RouterView中包含的视图。我想知道是否可以使用NavigationViewsNavigationLinks做这样的事情。 但是我真的不知道该怎么做,从哪里开始。

任何想法和帮助将不胜感激。

谢谢!

0 个答案:

没有答案