gpt4 book ai didi

iOS 如何最好地将用户对象传递到多个 View Controller (依赖注入(inject))

转载 作者:行者123 更新时间:2023-11-29 02:31:07 24 4
gpt4 key购买 nike

我有很多问题想在这里问,所以我希望有人能提供帮助,并帮助详细解释这个问题:

  1. 我有一个从服务器下载用户配置文件的用户对象。
  2. 我有一个自定义的 TabBarController,它有一个无限滚动的标签栏以及一个页面 View Controller ,它显示 10 个不同的 View Controller ,我想访问 User 对象中保存的不同信息。
  3. 截至目前,当调用 viewDidLoad 方法时,数据会延迟加载到 10 个 View Controller 中的每一个中。我现在需要做的是,不要延迟加载用户数据,而是一次下载所有数据,并让 10 个不同的 View Controller 访问用户对象中已下载的数据。

这是它的全部结构:

  • 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.

据我所知,在加载 10 个 View Controller 中的任何一个时,我应该写类似...

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

}

最佳答案

您需要的是将 View Controller 逻辑和方法与您从服务器拉取的数据源分开。

您不想预先实例化所有 Controller ,但您可以加载所有数据,因此当特定 View Controller 将被实例化时(当用户滚动或点击特定按钮时)- 该 View 的数据将很容易获得 Controller 。

关于iOS 如何最好地将用户对象传递到多个 View Controller (依赖注入(inject)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com