iOS 6 - (BOOL)应该没有调用导航控制器推送viewControllers的调用

时间:2012-10-10 20:28:30

标签: iphone objective-c ios xcode ios6

我的应用rootViewControllernavgationController

我找到了 推动控制器的

-(BOOL)shouldAutorotate没有被调用。

-(NSUInteger)supportedInterfaceOrientations只被调用一次。

我已在xcode's项目摘要(或plist)中正确检查了Windows所有方向支持。

我希望调用这些方法,因为有一些uicontrol定位代码,我想以编程方式执行方向更改。

我通过覆盖(类别)导航控制器的以下方法来解决这个问题

-(BOOL)shouldAutorotate;

-(NSUInteger)supportedInterfaceOrientations;

我检查了哪个控制器被推送,因此在导航控制器的以下方法中调用相应的推控制器的uicontrol定位代码

(NSUInteger)supportedInterfaceOrientations;

这很好但我不认为这是正确的方法。请帮助我寻求更好的解决方案。

5 个答案:

答案 0 :(得分:27)

您可以查看以下链接,您需要创建自定义导航以支持自动旋转

http://mobileappdevpage.blogspot.in/2012/11/how-to-use-should-autorotateios-6-with.html

您可以通过创建UINaviagationController类别

来实现此目的 .h文件的

代码是

@interface UINavigationController (autorotation)

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;

和.m文件的代码是

@implementation UINavigationController (autorotation)

-(BOOL)shouldAutorotate
{

    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    [self.topViewController shouldAutorotate];
    return YES;

}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;

}
@end

答案 1 :(得分:1)

我也遇到了与导航控制器相同的问题。在所有推送视图控制器的情况下,它工作正常,但我的情况完全不同我将一个viewcontroller作为根推入导航控制器(ViewControllerParent),

NavController
            -- rootViewController (ViewControllerParent)
                                          --- ViewControllerChild1
                                          --- ViewControllerChild2

由于某些项目要求,我将ViewControllerParent保持为基础,然后我根据用户操作将Child viewcontroller的视图添加为父视图。现在我有一个场景,我希望Child1不要旋转,Child2要旋转。

我遇到的问题是导航控制器类中的[self.topViewController]总是返回父对象,因为我没有将Childs推入导航堆栈。所以我的孩子们应该永远不会被调用。所以我不得不在父母的shouldAutorotate方法中进行类检查,然后返回旋转值。 我在父类(ViewControllerParent)中做了类似的事情,这是一种解决方法,但它修复了问题

-(BOOL)shouldAutorotate
{
  BOOL allowRotation = YES;

   if ([currentlyLoadedChild isKindOfClass:[Child1 class]])
   {
       allowRotation = NO;
   }
   if ([currentlyLoadedChild isKindOfClass:[Child2 class]])
   {
       allowRotation = YES;
   }
   return allowRotation;
} 

-anoop

答案 2 :(得分:0)

您可以通过

检查界面方向
[UIApplication sharedApplication].statusBarOrientation

在加载视图控制器时,例如,在viewWillAppear中。在那里,您可以进行子视图的布局。视图启动后,只要设备转动,就会调用shouldAutorotate

答案 3 :(得分:0)

重写UINavigationController是正确的方法,但我不确定你是否正确检查push控制器supportedInterfaceOrientations。

在这里查看我的答案:https://stackoverflow.com/a/12669343/253008

答案 4 :(得分:0)

我有同样的问题。检查this answer 它是一个很好的方法而不是应用ShouldAutoRotate