gpt4 book ai didi

ios - 处理具有两个不同 View 的 View Controller 的最佳方法: portrait and landscape

转载 作者:行者123 更新时间:2023-11-29 03:17:50 25 4
gpt4 key购买 nike

这个想法是根据设备方向处理两个不同的 View 。

基本上,如果我们排除 View , Controller 本身只是一段代码。它能够处理两个 View 中的任何一个 - 要么因为 IBOutlet 在两个 IB Storyboard View Controller 中具有相同的名称,要么因为它使用针对设备方向的 if 语句。

我尝试了两种不同的方法来做到这一点,但我仍然面临一个问题:我最终得到了两个实际的 Controller 实例,每个实例都运行相同的代码(因此两倍的线程和其他代码片段同时运行) .

我想摆脱一个,只运行一个实例。

你有什么建议?可能有更好的方法来处理我的用例,不是吗?

我的代码如下:

- (void)loadControllers
{
self.firstController = (DashboardViewController*)[[self storyboard] instantiateViewControllerWithIdentifier:@"DashboardPortrait" ];
self.firstController.isDashboard = NO;

self.secondController = (DashboardViewController*)[[self storyboard] instantiateViewControllerWithIdentifier:@"DashboardLandscape"];
self.secondController.isDashboard = YES;
}

- (void)viewDidLoad {
[super viewDidLoad];

[self loadControllers];

DashboardViewController *viewController = self.firstController;
[self addChildViewController:viewController];

viewController.view.frame = self.contentView.bounds;

[self.contentView addSubview:viewController.view];

self.currentViewController = viewController;
}

- (void)setOrientationPortrait:(BOOL)portrait {
DashboardViewController *viewController = portrait?self.firstController:self.secondController;


// Make sure the two view controllers share the same parent.
[self addChildViewController:viewController];

// If not same parent, dont transition
if (viewController.parentViewController!=self.currentViewController.parentViewController) return;

[self transitionFromViewController:self.currentViewController
toViewController:viewController
duration:0.0
options:UIViewAnimationOptionTransitionNone
animations:^{
[self.currentViewController.view removeFromSuperview];
viewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:viewController.view];
}
completion:^(BOOL finished) {
[viewController didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = viewController;


// Direct video to the destination, current controller
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];

[app faceRecognitionAttachPreview:self.currentViewController.previewView];
}
];

self.navigationItem.title = viewController.title;
}

最佳答案

根据 Apple ,如果两个方向之间存在显着差异,则最好为两个方向设置两个单独的 View Controller 。 They say :

If you want to present the same data differently based on whether a device is in a portrait or landscape orientation, the way to do so is using two separate view controllers. One view controller should manage the display of the data in the primary orientation (typically portrait), while the other manages the display of the data in the alternate orientation. Using two view controllers is simpler and more efficient than making major changes to your view hierarchy each time the orientation changes. It allows each view controller to focus on the presentation of data in one orientation and to manage things accordingly. It also eliminates the need to litter your view controller code with conditional checks for the current orientation.

关于ios - 处理具有两个不同 View 的 View Controller 的最佳方法: portrait and landscape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463732/

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