简体   繁体   English

在iOS中同时切换控制器和视图

[英]Switching both Controller and View in iOS

I have the following controllers and views in my project: 我的项目中有以下控制器和视图:

PortraitController.m
PortraitView.xib
LandscapeController.m
LandscapeView.xib

Portrait controller is used as a controller for the portrait view, and a landscape controller is used as a controller for the landscape view. 纵向控制器用作纵向视图的控制器,而横向控制器用作横向视图的控制器。

I need to switch BOTH controller and view when device orientation changes. 我需要同时切换两个控制器和查看设备方向更改的时间。 This approach is advised by Apple, but they do not provide an usable, general example of this! 苹果公司建议采用这种方法,但是他们没有提供可用的通用示例!

I have found various examples, but they are not quite what I need - one controller + two views in same xib, push+pop from navigation controller, place second view over previous, ... none of them is what I need. 我找到了各种示例,但它们并不是我所需要的-一个控制器+在同一个xib中的两个视图,导航控制器中的push + pop,第二个视图放置在以前的视图中,...我都不需要这些。

What is the simplest way to switch (with animation) between these two Controller+View combinations when device orientation changes? 当设备方向改变时,在这两个Controller + View组合之间切换(使用动画)的最简单方法是什么?

Is there a solution which can work for both iOS 5 and iOS 6? 是否有适用于iOS 5和iOS 6的解决方案?

The example that Apple provides uses the Storyboard's segue mechanism to go from one view to another, if you are using NIB files you'd need to go with the UINavigationViewController approach, you can even set the transition you want to go from one UIControllerView to another Apple提供的示例使用Storyboard的segue机制从一个视图转到另一个视图,如果使用NIB文件,则需要通过UINavigationViewController方法进行操作,甚至可以设置要从一个UIControllerView到另一个视图的过渡

For example, this code would switch from one UIViewController to another with a page turning animation 例如,此代码将通过翻页动画从一个UIViewController切换到另一个

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:yourViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:navController.view cache:NO];
[UIView commitAnimations];

To achieve the fact of knowing when to push one view or another (or use a segue if you switch to storyboards) you could register to listen to the UIDeviceOrientationDidChangeNotification event from your navigation controller or the current view, to register a certain method to handle screen rotation for example 为了实现知道何时推送一个视图或另一个视图(或者如果切换到情节提要时使用segue)的事实,可以注册以从导航控制器或当前视图监听UIDeviceOrientationDidChangeNotification事件,以注册某种方法来处理屏幕例如旋转

-(void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToChangeViewController) name:UIDeviceOrientationDidChangeNotification object:nil];

}

-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

It's very important to unregister the event 取消注册活动非常重要

Hope this can give you some insight on how to achieve what your are intending 希望这可以使您对如何实现自己的目标有一些见解

Here is a good approach for your problem: Rotating to Show Different iOS Views . 这是解决问题的一种好方法: 旋转以显示不同的iOS视图 With NSNotificationCenter you can manage the rotation event and push/pop a controller, or change any view you want. 使用NSNotificationCenter,您可以管理旋转事件并按下/弹出控制器,或更改所需的任何视图。 Hope this can help you. 希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM