繁体   English   中英

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

[英]Modal view controller not presented anymore after being presented 2/3 times

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

我有viewController调用ARViewController(以模式方式)。 如果我旋转2到3次,效果很好,但是不再调用此ARViewController,但是该应用程序仍在运行,没有崩溃,没有冻结。 该ARViewController由ARController初始化,ARController是一个类,用于计算增强现实所需的全部内容。 如果我在没有ARController的情况下调用此ARViewcontroller,则在视图控制器之间进行切换的效果非常好,将调用一个空白窗口,但至少我可以根据需要旋转设备。 因此,这必须来自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!

我不好,我无法解决这里的问题。

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

- (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];

好了,现在解决了,但是我不得不承认我对找到这种解决方案感到失望。

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

除非您确定每次切换为肖像时都会释放它们。

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

最后一件事,您向NavigationController展示了ModalViewController,但是您在[super presentingViewController]中将其关闭了,也许您可​​以添加这个?

暂无
暂无

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

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