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

iOS如何最好地将用户对象传递到多个视图控制器(依赖项注入),ios,objective-c,xcode,dependency-injection,Ios,Objective C,Xcode,Dependency Injection,我在这里有一个相当多的问题要问,所以我希望有人能提供帮助和帮助,详细解释这件事: 我有一个用户对象,可以从服务器下载用户的配置文件 我有一个自定义TabBarController,它有一个无限可滚动的tabbar,上面还有一个pageview控制器,它显示10个不同的视图控制器,我想访问用户对象中保存的不同信息 到目前为止,在调用viewDidLoad方法时,数据被延迟加载到10个视图控制器中的每一个。我现在需要做的不是延迟加载用户数据,而是一次下载所有数据,并让10个不同的视图控制器访问用户对

我在这里有一个相当多的问题要问,所以我希望有人能提供帮助和帮助,详细解释这件事:

  • 我有一个用户对象,可以从服务器下载用户的配置文件
  • 我有一个自定义TabBarController,它有一个无限可滚动的tabbar,上面还有一个pageview控制器,它显示10个不同的视图控制器,我想访问用户对象中保存的不同信息
  • 到目前为止,在调用viewDidLoad方法时,数据被延迟加载到10个视图控制器中的每一个。我现在需要做的不是延迟加载用户数据,而是一次下载所有数据,并让10个不同的视图控制器访问用户对象中已下载的数据
  • 以下是全部的结构:

    • CustomTabBarViewController

      PageContentViewController

      PageContentViewController保存对10个ViewController的引用,CustomTabBarViewController根据您滚动的方式或选择的选项卡栏指定显示哪一个

    据我所知,当加载10个视图控制器中的任何一个时,我应该编写如下内容:

     ViewController* vc = [[ViewController alloc] initWithUser:_user];
    
    显然,我需要创建这个init方法,但这通常是我想要做的,以便将用户对象传递到视图控制器中。请注意,我只想从服务器中提取一次用户数据

    更新

    选项卡视图控制器

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

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

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


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

    我理解这一点,但我正在寻找有关在加载特定视图控制器时如何访问用户类数据的代码。知道用户的数据是先加载的,并且只加载在选项卡BarViewControlleri中,我不确定我当时是否理解您的问题。TabbarViewController将保存用户数据的实例。PageViewController可以访问TabbarViewController并获取该数据。PageViewController还负责实例化各个页面并将数据的适当部分传递给它们。你能展示一些你的实际代码吗?我已经用实际代码更新了我的问题。我担心我可能做得不正确..我从下面的链接中得到了这个答案(我使用这个方法是因为我需要两个功能..能够滑动滚动到不同的视图控制器,以及在单击选项卡栏时能够跳转到特定的视图控制器):