![](/img/trans.png)
[英]Why is the modal view controller not being presented in applicationDidFinishLaunchingWithOptions?
[英]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.