iPhone呈现模态视图控制器时崩溃

Mic*_*all 30 iphone crash memory-management uiviewcontroller

我试图在另一个视图以模态方式呈现后直接显示模态视图(第二个是出现的加载视图).

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    LoadViewController *loader = [[LoadViewController alloc] init];
    [self presentModalViewController: loader animated:NO];
    [loader release];
}
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我得到一个"程序接收信号:"EXC_BAD_ACCESS"." 错误.

堆栈跟踪是:

0  0x30b43234 in -[UIWindowController transitionViewDidComplete:fromView:toView:]
1  0x3095828e in -[UITransitionView notifyDidCompleteTransition:]
2  0x3091af0d in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
3  0x3091ad7c in -[UIViewAnimationState animationDidStop:finished:]
4  0x0051e331 in run_animation_callbacks
5  0x0051e109 in CA::timer_callback
6  0x302454a0 in CFRunLoopRunSpecific
7  0x30244628 in CFRunLoopRunInMode
8  0x32044c31 in GSEventRunModal
9  0x32044cf6 in GSEventRun
10 0x309021ee in UIApplicationMain
11 0x00002154 in main at main.m:14
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我完全难过了!加载视图为空,因此肯定没有任何事情导致错误.是否与在同一个事件循环中以模态方式启动2个视图有关?

谢谢,

麦克风

编辑:很奇怪...我稍微修改了它,以便在一个小延迟后显示加载视图,这很好用!所以它似乎是在同一个事件循环中的东西!

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    [self performSelector:@selector(doit) withObject:nil afterDelay:0.1];
}

- (void)doit {
    [self presentModalViewController:loader animated:YES];  
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*all 30

我稍微修改了它,以便在一个小延迟后显示加载视图,这样工作正常!所以它似乎是在同一个事件循环中的东西!

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    [self performSelector:@selector(doit) withObject:nil afterDelay:0.1];
}

- (void)doit {
    [self presentModalViewController:loader animated:YES];  
}
Run Code Online (Sandbox Code Playgroud)

  • 我怀疑它也可以延迟0.0f,然后.基本上,它将在runloop的下一次迭代中呈现,基本上是即时的. (2认同)

Dan*_*iel 5

我相信我在iOS 4中重现了同样的错误.在我的应用程序中,当在显示第一个模态视图后立即尝试显示第二个模态视图时,崩溃始终发生.我挣扎了几个小时疯了.

在阅读了该主题中的帖子后,我尝试使用Tab Bar Application模板创建一个简单的可重现示例.我能够使用UIImagePickerController在响应"FirstViewController.m"中的按钮单击后显示第一个模态视图.当我试图再次显示UIImagePickerController时(在处理imagePickerControllerDidCancel消息之后),应用程序崩溃并出现相同的错误.

在设备上,根本不知道发生了什么.但是,当我在模拟器上运行代码时,我很幸运能在控制台上收到此消息:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal transition from to while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed'

因此,似乎我唯一的选择是遵循错误消息的建议,只需等到viewDidAppear(使用一个标志来表示我处于这种特殊模式),然后加载第二个模态视图.

这是完整性的完整堆栈跟踪:

** Call stack at first throw:
(
 0   CoreFoundation                      0x0238c919 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x024da5de objc_exception_throw + 47
 2   CoreFoundation                      0x02345078 +[NSException raise:format:arguments:] + 136
 3   Foundation                          0x000ab8cf -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
 4   UIKit                               0x00544317 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 212
 5   UIKit                               0x0035c769 -[UIViewController presentModalViewController:withTransition:] + 2937
 6   TestTempDelete                      0x000021cf -[FirstViewController showImagePicker] + 167
 7   Foundation                          0x0002fcea __NSFireDelayedPerform + 441
 8   CoreFoundation                      0x0236dd43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
 9   CoreFoundation                      0x0236f384 __CFRunLoopDoTimer + 1364
 10  CoreFoundation                      0x022cbd09 __CFRunLoopRun + 1817
 11  CoreFoundation                      0x022cb280 CFRunLoopRunSpecific + 208
 12  CoreFoundation                      0x022cb1a1 CFRunLoopRunInMode + 97
 13  GraphicsServices                    0x02bf12c8 GSEventRunModal + 217
 14  GraphicsServices                    0x02bf138d GSEventRun + 115
 15  UIKit                               0x002beb58 UIApplicationMain + 1160
 16  TestTempDelete                      0x00001eb4 main + 102
 17  TestTempDelete                      0x00001e45 start + 53

希望这可以帮助.