0

I have quite a question to ask here so I hope someone can be of assistance and help with a detailed explanation on the matter:

  1. I have a User object that downloads a user's profile from the server.
  2. I have a custom TabBarController which has a infinite scrollable tabbar as well as a pageview controller above it that displays 10 different view controllers that I want to access the different information that is held in the User object.
  3. As of right now, the data is lazy loaded in each of the 10 view controllers as their viewDidLoad method is called. What I need to do now is, instead of lazy loading the User data, is to download it all at once and have the 10 different view controllers access the already downloaded data in the User object.

Here is the structure of it all:

  • CustomTabBarViewController

    PageContentViewController

    The PageContentViewController holds a reference to the 10 ViewControllers and CustomTabBarViewController delegates which one it shows depending on which way you scroll or which tab bar you select.

From what I understand, when loading any of the 10 View Controllers I should write something like..

 ViewController* vc = [[ViewController alloc] initWithUser:_user];

I need to create this init method obviously but this is generally what I want to do to pass the user object into the View Controller. Mind you, I only want to pull the user data from the server once!

UPDATE

TabBarViewController

 -(void)setupContainerView
 {

_containerView = [[UIView alloc] initWithFrame:CGRectMake(0, scrollViewHeight, self.view.frame.size.width, self.view.frame.size.height - scrollViewHeight)];
_containerView.backgroundColor = Rgb2UIColor(230, 230, 230);
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_containerView];


//Load the view controllers from the storyboard and add them to the array.
UIStoryboard *storyboard = self.storyboard;

General_TableViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"GeneralTVC"];
Allergies_TableViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"AllergiesTVC"];
MedicalHistory_TableViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"MedicalHistoryTVC"];
Medications_TableViewController *vc4 = [storyboard instantiateViewControllerWithIdentifier:@"MedicationsTVC"];
FamilyHistory_TableViewController* vc5 = [storyboard instantiateViewControllerWithIdentifier:@"FamilyHistoryTVC"];
XRays_CollectionViewController* vc6 = [storyboard instantiateViewControllerWithIdentifier:@"XRaysCVC"];
Charts_CollectionViewController* vc7 = [storyboard instantiateViewControllerWithIdentifier:@"ChartsCVC"];
NextOfKin_TableViewController* vc8 = [storyboard instantiateViewControllerWithIdentifier:@"NextOfKinTVC"];
OrganDonor_TableViewController* vc9 = [storyboard instantiateViewControllerWithIdentifier:@"OrganDonorTVC"];
DoNotResuscitate_TableViewController* vc10 = [storyboard instantiateViewControllerWithIdentifier:@"DoNotResuscitateTVC"];


_subViewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vc9,vc10, nil];

}

PageContentViewController

 -(void)setupContainerView
 {

_containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_containerView.backgroundColor = [UIColor purpleColor];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_containerView];

//Load the view controllers from the storyboard and add them to the array.
UIStoryboard *storyboard = self.storyboard;

General_TableViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"GeneralTVC"];
Allergies_TableViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"AllergiesTVC"];
MedicalHistory_TableViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"MedicalHistoryTVC"];
Medications_TableViewController *vc4 = [storyboard instantiateViewControllerWithIdentifier:@"MedicationsTVC"];
FamilyHistory_TableViewController* vc5 = [storyboard instantiateViewControllerWithIdentifier:@"FamilyHistoryTVC"];
XRays_CollectionViewController* vc6 = [storyboard instantiateViewControllerWithIdentifier:@"XRaysCVC"];
Charts_CollectionViewController* vc7 = [storyboard instantiateViewControllerWithIdentifier:@"ChartsCVC"];
NextOfKin_TableViewController* vc8 = [storyboard instantiateViewControllerWithIdentifier:@"NextOfKinTVC"];
OrganDonor_TableViewController* vc9 = [storyboard instantiateViewControllerWithIdentifier:@"OrganDonorTVC"];
DoNotResuscitate_TableViewController* vc10 = [storyboard instantiateViewControllerWithIdentifier:@"DoNotResuscitateTVC"];


_subViewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vc9,vc10, nil];


vc1 = nil;
vc2 = nil;
vc3 = nil;
vc4 = nil;
vc5 = nil;
vc6 = nil;
vc7 = nil;
vc8 = nil;
vc9 = nil;
vc10 = nil;


_selectedViewController = [_subViewControllers objectAtIndex:self.pageIndex];


if (_selectedViewController.parentViewController == self)
{
    // nowthing to do
    return;
}

// adjust the frame to fit in the container view
_selectedViewController.view.frame = _containerView.bounds;

// make sure that it resizes on rotation automatically
_selectedViewController.view.autoresizingMask = _containerView.autoresizingMask;

// add as child VC
[self addChildViewController:_selectedViewController];

// add it to container view, calls willMoveToParentViewController for us
[_containerView addSubview:_selectedViewController.view];

// notify it that move is done
[_selectedViewController didMoveToParentViewController:self];

 }
Mike Simz
  • 3,976
  • 4
  • 26
  • 43

1 Answers1

1

What you need is to separate view controllers logic and methods from the data source you're pulling from the server.

You don't want to instantiate all controllers up front, but you can load all the data, so when particular view controller will be instantiated (when user scroll or tap on particular button) - data will be readily available for that view controller.

sha
  • 17,824
  • 5
  • 63
  • 98
  • I understand this, but I'm looking for the code on how to access the User classes data when a specific view controller is loaded. Knowing that the User's data is loaded first and only in the TabBarViewController – Mike Simz Nov 11 '14 at 16:19
  • I'm not sure I understand your problem then. TabbarViewController will hold an instance of user data. PageViewController has access to TabbarViewController and can get that data. PageViewController also responsible for instantiating individual pages and passing them proper parts of the data. Can you show some of your actual code? – sha Nov 11 '14 at 16:30
  • I've updated my question with the actual code. I'm worried I may be doing this incorrectly..I got this answer from the link below (I used this method because I needed 2 functions..the ability to swipe to scroll to different view controllers, as well as being able to jump to a specific view controller when a tab bar is clicked): http://stackoverflow.com/questions/13633059/uipageviewcontroller-how-do-i-correctly-jump-to-a-specific-page-without-messing – Mike Simz Nov 11 '14 at 16:38