在不受欢迎的视图触发交替景观

时间:2013-02-01 15:57:06

标签: iphone ios orientation uistoryboardsegue

我有一个包含图像和tableview的视图控制器。从这个视图控制器我将一个segue连接到一个包含全屏图像的横向视图(当你从Apple横向转动Stocks应用程序以全屏查看图形时使用相同的想法。)

通过以下方法调用此segue:

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self performSegueWithIdentifier: @"toGraph" sender: self];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        isShowingLandscapeView = NO;
    }
}

除此之外,当iphone处于纵向方向时,我还可以将桌面视图向下钻取更多级别。问题如下:在那些相应的级别中,当我将方向转换为横向时,仍然会触发到横向视图的segue!...我该怎么办才能发生这种情况?我只对从包含图像的第一个视图进入横向模式感兴趣。

提前谢谢你!

1 个答案:

答案 0 :(得分:1)

我不确定我的问题是否正确..但如果您的意思是“向下钻取表格视图”以深入了解导航控制器层次结构,您可以尝试以下方法。

这就是我在(我认为)类似情况下所做的事情:

的AppDelegate:

在.h:

@property (nonatomic) BOOL shouldAutorotate;

in .m:

//在didFinishLaunchingWithOptions中:

self.shouldAutorotate = NO;

//仍然在.m文件中

// Autorotation handling
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return self.shouldAutorotate ?
    UIInterfaceOrientationMaskAllButUpsideDown :
    UIInterfaceOrientationMaskPortrait;
}

导航控制器呈现人像控制器

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

纵向视图控制器(这里也是一个非常类似的segue处理):

在viewWillAppear中:

[(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldAutorotate:YES];

轮换处理:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Landscape View Controller(可能是您的全屏图片):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

更深入导航控制器层次结构(只需要肖像):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

看起来有些复杂,但这是唯一的方法,我设法让这些轮换工作在iOS5和6中正常工作。