You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI集成FirebaseAuth后,如何跳转至指定Storyboard页面?

SwiftUI集成FirebaseAuth后跳转至Storyboard视图控制器

我现有一个SwiftUI集成FirebaseAuth的项目,目前认证完成后会跳转至SwiftUI的HomeView,但需要改成跳转至名为main的Storyboard中、StoryboardID为MainView的MainViewController。

现有代码片段(SignInView):

import SwiftUI
import UIKit
import Combine
import FBSDKCoreKit
import FBSDKLoginKit
import FirebaseAuth

struct SignInView: View {
    @State var pushActive = false
    @ObservedObject private var viewModel: SignInViewModel
    
    init(state: AppState) {
        self.viewModel = SignInViewModel(authAPI: AuthService(), state: state)
    }
    
    
   var body: some View {
        VStack {
            NavigationLink(destination: HomeView(state: viewModel.state),
                           isActive: self.$pushActive) {
              EmptyView()
            }.hidden()
            VStack(alignment: .leading, spacing: 30) {
                Text("Log in")
                    .modifier(TextModifier(font: UIConfiguration.titleFont,
                                           color: UIConfiguration.tintColor))
                    .padding(.leading, 25)
                    .padding(.bottom, 80)
                VStack(alignment: .center, spacing: 30) {
                    VStack(alignment: .center, spacing: 25) {
                        CustomTextField(placeHolderText: "E-mail",
                                      text: $viewModel.email)
                        CustomTextField(placeHolderText: "Password",
                                      text: $viewModel.password,
                                      isPasswordType: true)
                    }.padding(.horizontal, 25)
                    
                    VStack(alignment: .center, spacing: 40) {
                        customButton(title: "Log In",
                                     backgroundColor: UIConfiguration.tintColor,
                                     action: { self.viewModel.login() })
                      
                    }
                }
            }
            Spacer()
        }.alert(item: self.$viewModel.statusViewModel) { status in
            Alert(title: Text(status.title),
                  message: Text(status.message),
                  dismissButton: .default(Text("OK"), action: {
                    if status.title == "Successful" {
                        self.pushActive = true
                    }
                  }))
        }
    }
    
    private func customButton(title: String,
                              backgroundColor: UIColor,
                              action: @escaping () -> Void) -> some View {
        Button(action: action) {
            Text(title)
                .modifier(ButtonModifier(font: UIConfiguration.buttonFont,
                                         color: backgroundColor,
                                         textColor: .white,
                                         width: 275,
                                         height: 55))
        }
    }
}

需要修改的跳转代码部分:

NavigationLink(destination: HomeView(state: viewModel.state),
                           isActive: self.$pushActive) {

解决方案

有两种可行的方式实现需求:

方式一:用UIViewControllerRepresentable包装Storyboard视图控制器

这种方式可以继续沿用SwiftUI的NavigationLink跳转逻辑,需要先把UIKit的MainViewController包装成SwiftUI可识别的View:

  1. 创建包装器:
struct MainViewControllerWrapper: UIViewControllerRepresentable {
    typealias UIViewControllerType = MainViewController
    
    func makeUIViewController(context: Context) -> MainViewController {
        // 加载指定名称的Storyboard
        let storyboard = UIStoryboard(name: "main", bundle: nil)
        // 通过StoryboardID获取目标视图控制器
        guard let mainVC = storyboard.instantiateViewController(withIdentifier: "MainView") as? MainViewController else {
            fatalError("无法找到StoryboardID为MainView的视图控制器")
        }
        return mainVC
    }
    
    func updateUIViewController(_ uiViewController: MainViewController, context: Context) {
        // 若需要动态更新视图控制器内容,可在此实现逻辑
    }
}
  1. 修改NavigationLink的目标视图:
    将原来的HomeView(state: viewModel.state)替换为包装后的视图:
NavigationLink(destination: MainViewControllerWrapper(),
               isActive: self.$pushActive) {
    EmptyView()
}.hidden()

方式二:直接用UIKit逻辑切换根视图控制器

如果不需要保留SwiftUI的导航栈,这种方式更直接,直接替换应用的根视图控制器:

修改Alert的确认按钮动作:

Alert(title: Text(status.title),
      message: Text(status.message),
      dismissButton: .default(Text("OK"), action: {
        if status.title == "Successful" {
            // 加载目标Storyboard和视图控制器
            let storyboard = UIStoryboard(name: "main", bundle: nil)
            guard let mainVC = storyboard.instantiateViewController(withIdentifier: "MainView") as? MainViewController else {
                return
            }
            // 获取当前窗口并设置根视图
            if let window = UIApplication.shared.windows.first {
                window.rootViewController = mainVC
                window.makeKeyAndVisible()
            }
        }
      }))

同时可以移除原来的NavigationLink相关代码,因为不再依赖它完成跳转。


内容的提问来源于stack exchange,提问作者Medic305

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:57:43