- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有很多问题想在这里问,所以我希望有人能提供帮助,并帮助详细解释这个问题:
这是它的全部结构:
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/
我是一名优秀的程序员,十分优秀!