iOS如何最好地将用户对象传递到多个视图控制器(依赖注入)

时间:2014-11-11 16:08:10

标签: ios objective-c xcode dependency-injection

我有一个问题要问这里,所以我希望有人可以提供帮助并帮助详细解释这个问题:

  1. 我有一个User对象,可以从服务器下载用户的个人资料。
  2. 我有一个自定义TabBarController,它有一个无限可滚动的tabbar以及它上面的一个pageview控制器,它显示了10个不同的视图控制器,我想访问User对象中保存的不同信息。
  3. 截至目前,当调用viewDidLoad方法时,数据在每个10个视图控制器中都是延迟加载的。我现在需要做的是,不是延迟加载用户数据,而是一次性下载所有数据,并让10个不同的视图控制器访问User对象中已经下载的数据。
  4. 这是所有结构:

    • CustomTabBarViewController

        

      PageContentViewController

           

      PageContentViewController包含对10个ViewControllers和CustomTabBarViewController委托的引用,具体取决于您滚动的方式或选择的标签栏。

    根据我的理解,当加载10个View控制器中的任何一个时,我应该写一些像......

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

    我需要显然创建这个init方法,但这通常是我想将用户对象传递给View Controller的目的。请注意,我只想从服务器中提取用户数据一次!

    更新

    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];
    
     }
    

1 个答案:

答案 0 :(得分:1)

您需要的是将视图控制器逻辑和方法与您从服务器提取的数据源分开。

您不希望事先实例化所有控制器,但您可以加载所有数据,因此当实例化特定视图控制器时(当用户滚动或点击特定按钮时) - 该视图的数据将随时可用控制器。