skip to Main Content

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?

2

Answers


  1. 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
           }
       }
    }
    

    enter image description here

    Login or Signup to reply.
  2. 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 e.g Home and then passing to the required VC on click.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search