模态视图控制器在呈现2/3次后不再显示

时间:2012-10-24 14:35:57

标签: ios modalviewcontroller augmented-reality

这就是我必须将增强现实功能集成到应用程序中。将它测试后,现在我已经准备好将它集成。该应用程序始终以纵向运行,我决定在向右或向左旋转iPhone时显示增强现实(当然,如果我回到纵向,则显示原始视图控制器)。事实上,增强现实是以模态呈现的。

我有viewController调用ARViewController(模态)。如果我旋转2或3次,它工作正常,但之后不再调用此ARViewController,但应用程序仍在运行,没有崩溃,没有冻结。这个ARViewController是用ARController初始化的,这是一个计算增强现实所需的所有类。如果我在没有ARController的情况下调用这个ARView控制器,在视图控制器之间切换工作非常精细,将调用一个空白窗口,但至少我可以根据需要旋转设备。所以,这必须来自ARController,我记录了自己的内存泄漏(通过我使用ARC的方式),我认为这可能是问题的原因,因为我多次使用这个ARController。

但在进一步讨论之前,我想知道我是否做了任何可能通过在视图控制器之间切换而影响ARController的错误:

以下是我在viewController中调用ARViewController的方法:

if (orientation == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"ViewController Landscape left");
        ARViewController *arVC = [[ARViewController alloc] initWithNibName:@"ARViewController" bundle:nil];
        [self setCameraViewController:arVC];
        [arVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
        [[self navigationController] presentModalViewController:cameraViewController animated:YES];
        arVC = nil;
    }
    else if (orientation == UIDeviceOrientationLandscapeRight) {
        NSLog(@"ViewController Landscape Right");
        ARViewController *arVC = [[ARViewController alloc] initWithNibName:@"ARViewController" bundle:nil];
        [self setCameraViewController:arVC];
        [arVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
        [[self navigationController] presentModalViewController:cameraViewController animated:YES];
        arVC = nil;
    }

ARViewController的初始化:

- (void)viewDidLoad {
[super viewDidLoad];
self.arController = [[ARController alloc] initWithViewController:self];
arController.filterDiscover = filterDiscover;
arController.filterEat = filterEat;
arController.filterSleep = filterSleep;

// Listen for changes in device orientation
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

//if ViewController presents this modal ARViewController
if(!arController.filterDiscover && !arController.filterEat && !arController.filterSleep)
    [arController callAlertViewToFilter];
else
    [arController loadData];
}

最后,如果我在ARViewController中旋转到Portrait,我将回到原始视图控制器:

if (orientation == UIDeviceOrientationPortrait){
    NSLog(@"ARViewController Portrait");
    [self setArController:nil];
    [[super presentingViewController] dismissModalViewControllerAnimated:YES];
}

我试图尽可能清楚,如果有人遇到类似这样的问题,可能会有一些解决这个问题的线索。我本可以显示ARController的代码,但它有点太长了,现在我只想知道这里是否有什么问题。如果需要我会展示它。

这可能会有所帮助,当ARViewController不再显示时,我在调试区域找到了这个输出:

2012-10-24 17:57:51.072 beiceland[20348:907] ViewController Landscape Right
2012-10-24 17:57:51.073 beiceland[20348:907] Warning: Attempt to present <ARViewController: 0x203f0c60> on <RevealController: 0x1cd5dca0> while a presentation is in progress!

2 个答案:

答案 0 :(得分:1)

我很糟糕我在这里摆脱了解决方案。

我使用了调试器和断点并重复了关键序列,我发现我没有进入:

- (void)deviceOrientationDidChange:(NSNotification *)notification

了。所以这是我第一次看到这样的东西,设备方向改变的听众突然停止射击。因此,我的解决方案非常残酷,但至少它具有停止问题的优点,只是在检测到设备方向改变后我停止了听众:

if (orientation == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"ViewController Landscape left");
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        ARViewController *arVC = [[ARViewController alloc] initWithNibName:@"ARViewController" bundle:nil];
        [self setCameraViewController:arVC];
        arVC = nil;
        [cameraViewController setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
        [[super navigationController] presentModalViewController:cameraViewController animated:YES];
}

每次我使用viewController(调用视图控制器)时,我都会重新初始化监听器,这意味着在viewDidAppear:

// Listen for changes in device orientation
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

现在它已经解决但我不得不承认我对找到这样的解决方案感到失望。

答案 1 :(得分:0)

好吧,我之前从未使用过ARC,但根据我所看到的,每次更改方向(在横向上)时都会初始化ARViewController。为什么不直接实例化2个ARViewController,并且每次都调用它们?

除非您确定每次切换到肖像时都会释放这些内容。

另外,为什么不使用 pushViewController:animated:

最后一件事,你将nowModalViewController呈现给你的导航控制器,但你在[super presentsViewController]中将它解雇了,也许你可以添加这个?

相关问题