简体   繁体   中英

How to pass data among Navigation bar, Tab bar, View Controller

I first use GoogleSignIn SDK for login phrase. Then, I use the following code for redirecting to the homepage of my apps. self.present(tabController, animated: true, completion: nil)

Here is the structure of my app (I use storyboard):

TabBarController

  1. NavigationBarController (Homepage): View Controllers
  2. NavigationBarController (Function 1): View Controllers
  3. NavigationBarController (Function 2): View Controllers
  4. NavigationBarController (Profile): View Controllers

Now that I have logged in, I would like to pass the data from google to my profile root view controller. Yet, I am not sure how to do that.

Here is my current code, that I have put in func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!):

let tabController: UITabBarController = self.storyboard?.instantiateViewController(withIdentifier: "homepageVC") as! UITabBarController
        let navController: UINavigationController = self.storyboard?.instantiateViewController(withIdentifier: "homepageVC2") as! UINavigationController
        let vc = navController.viewControllers[0] as! ViewController

        tabController.modalPresentationStyle = .fullScreen
        vc.name = fullName
        self.present(tabController, animated: true, completion: nil)

** where fullName is the global variable in my LoginViewController, which contains user.fullName (from google).

But of coz, it does not work. I hope someone can help me out with this. Many thanks.

Heres what I do with signins with products like Firebase,

Make a class that handles all data for the user

class UserClass: {
   static var name: String = ""
   static var email: String = ""
   static var UID: String = ""
   static var youGetTheIdea: String = ""
   static var some_array: [String] = [""]

}

You can then call this from any class by calling,

//To set var
UserClass.name = Auth().Auth.currentuser.username
//To get var
print(UserClass.name)

I know there are differing opinions on static var(s) but this is okay as long as you do data validation before moving to the next view.

You might want to give this a read for more options with good explanations

https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM