旋转iPhone,并实例化一个新的UIViewController? [英] Rotating the iPhone, and instantiating a new UIViewController?

查看:81
本文介绍了旋转iPhone,并实例化一个新的UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下内容。当用户旋转iPhone时,我想实例化一个新的UIViewController(在旋转时自动,不是单击按钮或执行类似的操作),并向用户显示这个新的UIViewController以横向方向处理的视图。如何正确地执行此操作?

I would like to implement the following. When the user rotates the iPhone, I want to instantiate a new UIViewController (automatically upon rotation, not clicking a button or performing a similar action) and show to the user a view handled by this new UIViewController in landscape orientation. How to do this properly ?

我尝试在方法willRotateToInterfaceOrientation和didRotateFromInterfaceOrientation中实例化新控制器,但这些方法都没有被调用!我怀疑这是因为当前控制器是由导航控制器推入的,导航控制器本身由tabBarController处理。任何线索?我们非常感谢一个简单的代码片段。

I tried to instantiate the new controller in the methods willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation, but none of this methods gets called!. I suspect this is because the current controller is pushed in by a navigation controller which is itself handled by a tabBarController. Any clue? A simple code snippet would be greatly appreciated.

提前谢谢。

推荐答案

我在应用程序上实现了这种确切类型的行为,关键是确保任何父控制器实现shouldAutorotateToInterfaceOrientation并且您当前的视图控制器也实现它。在我的情况下,我使用一个标签控制器拦截对shouldAutorotateToInterfaceOrientation的调用,我不得不覆盖标签控制器以使调用通过。视图控制器的默认行为是仅以纵向模式显示。

I've implemented this exact type of behavior on an app and the key is to make sure that any parent controllers implement shouldAutorotateToInterfaceOrientation and that your current view controller also implements it. In my case I am using a tab controller which intercepts the call to shouldAutorotateToInterfaceOrientation and I had to override the tab controller to get the call to fall through. The default behavior of view controllers is to display in portrait mode only.

因此,您需要强制它们允许所有方向更改:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

So you need to force them to allow all orientation changes through: -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }

然后,为了加载新的视图控制器,您应该回复:

Then in order to load a new view controller you should respond to:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
  if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) ||
     (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
  {    
    // Load the view controller you want to display in landscape mode...
  }
}

也可以使用:

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

检测方向变化即将到来。

To detect that the orientation change is coming.

在我的情况下,我使用didRotateFromInterfaceOrientation来检查最后一个方向是否是纵向,如果是,请加载我希望以横向模式显示的视图。

In my case I am using didRotateFromInterfaceOrientation to check if the last orientation was portrait and if so load the view I want displayed in landscape mode.

然后我实施在我正在加载的viewcontroller中使用相同的方法,它负责检测方向何时更改为纵向并从视图堆栈中解除其自身。

I then implemented the same methods in the viewcontroller I am loading and it is responsible for detecting when the orientation changes back to portrait and dismissing itself from the view stack.

希望有助于很少。

保罗

这篇关于旋转iPhone,并实例化一个新的UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆