我认为目前我有一个相当简单的应用程序,它基于一些拼凑在一起的教程。我在 OSX 10.6.4 中使用 XCode 3.2.3。它最初是一个标准的 iPhone“基于窗口的应用程序”。使用界面生成器,我使用此处的 O'Reilly 视频教程添加了一个选项卡栏 Controller :
http://broadcast.oreilly.com/2009/06/tab-bars-and-navigation-bars-t.html
在第一个选项卡中,我有一个带有两个按钮的标准 UIView。两者都调用相同的函数来显示 UIImagePickerController:
-(IBAction) btnPhotoClicked:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if((UIButton *)sender == btnChoosePhoto)
{
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
我在模拟器中运行代码,因此只需单击名为“选择照片”的按钮。当对话被选择的照片释放时,这个函数运行:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *mediaUrl;
mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
if (mediaUrl == nil)
{
imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
if(imagePuzzle == nil)
{
//--- Original Image was selected ---
imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
}
else {
//--- Get the edited image ---
//--- If it was successful the above valueForKey:UIImagePickerControllerEditedImage
//--- would have assigned it already.
}
}
else {
//--- Muppet selected a video
}
// Animate the picker window going away
[picker dismissModalViewControllerAnimated:YES];
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.delegate = self;
[self presentModalViewController:imageViewController animated:YES];
[imageViewController release];
}
这就是我的问题所在。我已经尝试了许多不同的 hack 和迭代,但上面的代码是最简单的问题。当 imageViewController 显示为模态对话框时,抛出以下异常:
2010-07-09 15:29:29.667 Golovomka[15183:207] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal
transition from <NewViewController: 0x5915f80> to <ImageViewController: 0x594a350>
while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear
to know the current transition has completed'
我该如何治疗?我尝试过延迟和其他技巧,但并不真正理解我应该如何使用 viewDidAppear 或 viewDidDisappear 来帮助我。还要注意的是,一个非常基本的应用程序,一个 View 加载选择器,然后显示另一个带有图像的 View 不会产生错误。非常感谢收到任何帮助。
最佳答案
要解决此处描述的特定问题,您可以在类中添加 viewDidAppear 方法:
-(void)viewDidAppear:(BOOL)animated
{
if (/*just visited ImagePicker*/)
{
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.delegate = self;
[self presentModalViewController:imageViewController animated:YES];
[imageViewController release];
}
}
从您的电话下方删除这些行:
[picker dismissModalViewControllerAnimated:YES];
因此,每当您的类 self
出现(显示)时,它都会调用 viewDidAppear
... 因为这很可能不是真的 你一直想要什么,你可以添加一些变量来设置/清除定义是否在显示 self
时立即显示 imageViewController
。类似于“如果来自图像选择器,则显示 imageViewController,否则什么都不做”。
也就是说,恕我直言,推送模态视图通常应该是响应用户操作而完成的,我可能会重新考虑这里的用户体验 - 例如添加一个 subview 而不是推送模态视图,您可以在当前拥有代码的地方执行此操作 - 但如果您只是在玩一些应该解决 NSInternalInconsistencyException 的教程。 :) 干杯!
关于iphone - iPhone 应用程序中的异常 : Modal transition is already in progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213734/