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
weakfor 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(notlet) 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
LoginViewControllerto theUITabBarController(not toNewsViewControlleritself) - Verify the segue's identifier matches exactly what you're using in code (no typos—case-sensitive!)
- Check that
NewsViewControlleris 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.loginUseris not nil before passingnewsVCis 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

