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

Swift中如何将LoginUser对象从登录控制器传递到TabBar子控制器?

Hey there! Let's figure out why your LoginUser object is showing up as nil in NewsViewController after the segue. This is a common gotcha with tab bar controllers, so let's break down the likely issues and fixes:

1. You're accessing the tab bar's child controller incorrectly (most common fix)

UITabBarController's child view controllers might not be fully initialized when you try to access selectedViewController in prepare(for:sender:). Instead of relying on the selected controller, directly fetch the NewsViewController from the tab bar's viewControllers array to ensure you're targeting the right instance.

Here's a corrected prepare(for:sender:) implementation:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard segue.identifier == "LoginToTabBarSegue" else { return }
    
    // Get the destination tab bar controller
    guard let tabBarController = segue.destination as? UITabBarController else {
        print("Could not cast destination to UITabBarController")
        return
    }
    
    // Find the NewsViewController in the tab bar's child controllers
    guard let newsVC = tabBarController.viewControllers?.first(where: { $0 is NewsViewController }) as? NewsViewController else {
        print("Could not find NewsViewController in tab bar controllers")
        return
    }
    
    // Pass the logged-in user (double-check this isn't nil here!)
    guard let loggedInUser = self.loginUser else {
        print("loginUser is nil in LoginViewController")
        return
    }
    newsVC.currentUser = loggedInUser
}

2. The NewsViewController property is misconfigured

Double-check how you've declared the user property in NewsViewController:

  • Avoid using weak for this property (unless you have a specific cycle reference concern—unlikely here, since we're passing a newly created user object)
  • Make sure it's a var (not let) so it can be set after initialization

Wrong:

weak var currentUser: LoginUser? // Weak will cause the object to be deallocated

Right:

var currentUser: LoginUser? // Mutable, strong reference to hold the user

Also, ensure you're not trying to access currentUser too early (like in init?(coder:)). Wait until viewDidLoad or viewWillAppear to use the value, since the property gets set during the segue preparation before the view loads.

3. Storyboard segue setup errors

  • Confirm your segue is directly connected from LoginViewController to the UITabBarController (not to NewsViewController itself)
  • Verify the segue's identifier matches exactly what you're using in code (no typos—case-sensitive!)
  • Check that NewsViewController is actually added as a child controller in the tab bar's view controller list in Storyboard

4. You're manually instantiating the tab bar controller instead of using the segue

If you're creating the tab bar controller via code (instead of triggering the segue), the prepare(for:sender:) method won't run, and your user object won't be passed. Make sure you're triggering the segue properly after login success:

// Correct: Trigger the segue defined in Storyboard
performSegue(withIdentifier: "LoginToTabBarSegue", sender: self.loginUser)

// Wrong: This creates a new, unconfigured tab bar controller instance
// let tabBarVC = storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
// present(tabBarVC, animated: true)

Quick debugging tip

Add print statements in prepare(for:sender:) to confirm:

  • self.loginUser is not nil before passing
  • newsVC is successfully found in the tab bar's controllers

This will help you narrow down exactly where the nil value is creeping in.

内容的提问来源于stack exchange,提问作者艁ukasz Betta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:33:54