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

Swift多Storyboard登录后跳转首页,如何去除返回/模态弹窗

登录后无返回页面切换的解决方案

问题场景

项目拆分了LogIn和ServiceHome两个Storyboard,未使用默认Main Storyboard。启动时通过SceneDelegate读取UserDefaults的登录标识设置对应根控制器,但登录成功后,用pushViewController或present跳转至首页时,要么出现返回按钮可回到登录页,要么首页以模态弹窗形式展示,无法实现登录后无返回的页面切换。

现有关键代码

SceneDelegate启动逻辑

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let _ = UserDefaults.standard.string(forKey: kIsLogged) {
        print("I'm loggin")
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        window?.makeKeyAndVisible()
        
        let storyboard = UIStoryboard(name: "ServiceHomeViewStoryBoard", bundle: nil)
        window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "SB_ServiceHome")
    } else {
        print("I'm not loggin")
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        window?.makeKeyAndVisible()
        
        let storyboard = UIStoryboard(name: "LogIn", bundle: nil)
        window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "LogInStoryBoard")
    }
}

当前登录成功跳转代码(存在问题)

let storyBoard = UIStoryboard(name: "ServiceHomeViewStoryBoard", bundle: nil)
let serviceHomeViewController = storyBoard.instantiateViewController(withIdentifier: "SB_ServiceHome")
self.navigationController?.pushViewController(serviceHomeViewController, animated: true)
self.navigationController?.present(serviceHomeViewController, animated: true)
self.present(serviceHomeViewController, animated:true, completion:nil)

解决方案

核心思路是直接替换UIWindow的根控制器,彻底切断登录页面与首页的关联,而非在现有导航栈中添加或模态展示页面。

修改后的登录成功代码

// 1. 更新登录状态到UserDefaults,确保下次启动直接进入首页
UserDefaults.standard.set("已登录", forKey: kIsLogged)
UserDefaults.standard.synchronize()

// 2. 获取当前应用的主窗口
guard let window = UIApplication.shared.connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .filter({$0.isKeyWindow}).first else {
    return
}

// 3. 实例化首页控制器
let storyBoard = UIStoryboard(name: "ServiceHomeViewStoryBoard", bundle: nil)
let serviceHomeViewController = storyBoard.instantiateViewController(withIdentifier: "SB_ServiceHome")

// 4. 替换根控制器,可选添加过渡动画提升体验
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
    window.rootViewController = serviceHomeViewController
}, completion: nil)

说明

  • 替换根控制器后,登录页面的控制器会被系统释放,不会保留在内存中,自然不存在返回路径
  • 使用UIView.transition添加过渡动画,可根据需求修改动画类型(比如.transitionFlipFromRight等)
  • 必须先更新UserDefaults的登录状态,保证下次启动直接进入首页

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 03:22:10