简体   繁体   中英

Is it possible to transfer data retrieved from Firebase in the tab bar controller class from the tab bar controller to a view controller? Xcode, Swift

So, I want to prefetch some of the data needed in the view controllers associated with the tab bar controller as the user moves from the login page to the home page (tab bar controller exists between these two view controllers). I'm fetching the data in a custom TabBarController class and using the following code to send it (doesn't work):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    if segue.destination is Profile { // Profile = one of the view controllers in the tab bar
            let vc = segue.destination as? Profile
            vc?.uid = self.userID
            vc?.name = self.name
            vc?.skills = self.skillSet
    }
    
}

What is the best way to do this? Please note I can't prefetch the data before the tab bar controller as that is the login page. If I prefetch the profile page data on the home page (tab bar controller {home, search, profile}), how can I transfer it to another view controller (to profile) in the same tab bar?

What is the best way to do this?

  • I believe once you perform login call (Inside login screen) you will have userID and other data related to that user, so inside Login controller you have added code for making Tabbar controller as initial controller.

  • So based on that best thing is to store userData into tabbar controller .

  • So once you sore data into tabbar controller (from either login or Home Screen) you can fetch user data in any root controller (home, search, profile) which is inside tab-bar by below code.

struct User { var userID: String? }

class TabbarController: UITabBarController {
    var userData: User
}

class HomeController: UIViewController {
 var userData: User?
   override func viewWillAppear(_ animated: Bool) {
       if let tabBar =  self.tabBarController as? TabbarController {
        self.userData = tabBar.userData
       }
   }
}

在此处输入图片说明

There are multiple ways you can achieve this.

  1. Using SuperViewController,
  2. Using SingleTone class if you want to access in other than TabbarViewVontroller subclass.
  3. Saving in a common location like in constants if data is small.
  4. Prefetching data in one VC eg Home and then passing to the required VC on click.

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