【iOS】SwiftUI 路由管理(NavigationStack)

QDRouter.swift

swift 复制代码
import SwiftUI

@MainActor
class QDRouter: ObservableObject {
    @Published var path = NavigationPath()

    static let main = QDRouter() // 单例

    private init() {}

    func open(_ url: String) {
        guard let url = URL(string: url) else {
            return
        }
        UIApplication.shared.open(url)
    }

    func push(page: RoutePage, param: [String: String]? = nil) {
        path.append(QDRoute(page: page, param: param))
    }

    func pop() {
        path.removeLast()
    }

    func popToRoot() {
        path.removeLast(path.count)
    }
}

// MARK: route page

enum RoutePage {
    case none
    case center
    case web
}

struct QDRoute: Hashable {
    var page: RoutePage
    var param: [String: String]?
}

// MARK: present view

enum PresentPage {
    case none
    case web
}

class PresentObject: ObservableObject {
    @Published var pageName: PresentPage = .none
    @Published var isPresent: Bool = false
    @Published var param: [String: String]?

    func presentView(pageName: PresentPage, param: [String: String]? = nil, isPresent: Bool = true) {
        self.pageName = pageName
        self.param = param
        self.isPresent = isPresent
    }
}

extension View {
    func withNavDestination() -> some View {
        return navigationDestination(for: QDRoute.self) { route in
            let param = route.param
            switch route.page {
            case .center:
                CenterView(param: param)
            case .none:
                Text("")
            case .web:
                WebView()
            }
        }
    }

    func withPresentDestination(isPresent: Binding<Bool>, pageName: PresentPage, param: [String: String]?) -> some View {
        return fullScreenCover(isPresented: isPresent, onDismiss: {
            print("")
        }, content: {
            switch pageName {
            case .web:
                WebView(param: param)
            case .none:
                Text("")
            }
        })
    }
}

初始页面:

LaunchView.swift

swift 复制代码
import SwiftUI

/// 启动视图
struct LaunchView: View {
    @State private var logoOpacity: Double = 0.0

    @State var isPreview = false

    @StateObject var router = QDRouter.main

    @State var showCenter = false

    @StateObject private var presentObject = PresentObject()

    var body: some View {
        NavigationStack(path: $router.path) {
            VStack {
                if showCenter {
                    CenterView()
                } else {
                    ZStack(alignment: Alignment.bottom, content: {
                        ZStack {
                            Image("startImageNew")
                                .resizable()
                                .edgesIgnoringSafeArea(.all)
                            Image("launchTopLogo").opacity(logoOpacity)
                        }
                        Image("launchBottomLogo")
                    })
                    .onAppear {
                        let duration = 0.5
                        withAnimation(.easeIn(duration: duration)) {
                            logoOpacity = 1.0
                        }
                        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
                            print("动画完成")
                            if !isPreview {
                                self.showCenter = true
                            }
                        }
                    }
                }
            }.withNavDestination()

        }.environmentObject(presentObject)
    }
}

struct LaunchView_Previews: PreviewProvider {
    static var previews: some View {
        LaunchView(isPreview: true)
    }
}

由 LaunchView跳转到CenterView

CenterView.swift

swift 复制代码
import SwiftUI

struct CenterView: View {
    @State private var selectedTab = 0
    @EnvironmentObject var presentObject: PresentObject

    var param: [String: String]?
    var body: some View {
        TabView(selection: $selectedTab) {
            FirstView()
                .tabItem {
                    (selectedTab == 0) ? Image(systemName: "house.fill") : Image(systemName: "house")
                    Text("首页")
                }.tag(0)

            SecondView()
                .tabItem {
                    Image(systemName: "dollarsign.circle")
                    Text("财富")
                }.tag(1)

            ThirdView()
                .tabItem {
                    Image(systemName: "wallet.pass")
                    Text("钱包")
                }.tag(2)

            FourthView()
                .tabItem {
                    Image(systemName: "person")
                    Text("个人")
                }.tag(3)
        }
        .navigationBarBackButtonHidden(true)
        .onAppear {}
        .withPresentDestination(isPresent: $presentObject.isPresent, pageName: presentObject.pageName, param: presentObject.param)
    }
}

#Preview {
    CenterView(param: [:])
}

withNavDestination 用于控制路由的push和pop

withPresentDestination 用于控制present view

具体使用:

swift 复制代码
@EnvironmentObject var presentObject: PresentObject
    var body: some View {
        VStack {
            Text("Hello, World 4")
            Button("present view") {
                presentObject.presentView(pageName: .web)
            }

            Button("push view") {
                QDRouter.main.push(page:.web)
            }
        }
    }
相关推荐
战族狼魂6 小时前
XCode 发起视频 和 收到视频通话邀请实现双语功能 中文和俄语
swift
Sim14808 小时前
iPhone将内置本地大模型,手机端AI实现0 token成本时代来临?
人工智能·ios·智能手机·iphone
UXbot10 小时前
2026年AI全链路产品开发工具对比:5款从创意到上线一站式平台深度解析
前端·ui·kotlin·软件构建·swift·原型模式
Digitally10 小时前
如何将 iPad 上的照片传输到 U 盘(4 种解决方案)
ios·ipad
报错小能手13 小时前
ios开发方向——swift并发进阶核心 @MainActor 与 DispatchQueue.main 解析
开发语言·ios·swift
LcGero13 小时前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb
ii_best13 小时前
lua语言开发脚本基础、mql命令库开发、安卓/ios基础开发教程,按键精灵新手工具
android·ios·自动化·编辑器
用户223586218202 天前
WebKit WebPage API 的引入尝试与自研实现
ios
啦啦啦!2 天前
ChatGPT和Gemini的接入和封装
人工智能·ios·chatgpt
报错小能手2 天前
ios开发方向——swift并发进阶核心 async/await 详解
开发语言·ios·swift