“应用程序尝试以模态方式呈现一个主动控制器”尽管安装了各种安全防护装置

时间:2014-01-26 02:30:05

标签: ios iphone ios7 uiimagepickercontroller

当我的控制器试图呈现模态UIImagePickerController时,我偶尔会收到这个例外(大约20%的时间)。我已经对SO进行了相当多的研究,并遵循了similar questions关于提出各种保护措施的说明。虽然这可以减少我得到这个错误的频率,但我仍然收到它并且不确定我能做些什么来阻止它发生。

我的设置如下:我的根控制器是一个Tab Bar控制器,然后我嵌入了一个导航控制器。在这个导航控制器中我有TakePhoto1Controller,当有人想要拍照时,我会以模态方式显示我的ImagePickerController

我的代码如下:

BOOL modalPresent = (BOOL)(self.presentedViewController);
    //Present the Camera UIImagePicker if no image is taken
    if (!appDelegate.imageStorageDictionary[@"picture1"]){
        if (modalPresent == NO){ //checks if the UIImagePickerController is already modally active
            if (![[self imagePickerController] isBeingDismissed]) [self dismissViewControllerAnimated:NO completion:nil];
            [self presentViewController:self.imagePickerController animated:NO completion:nil];
        }
    }

我得到的错误如下:

“因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'应用程序尝试以模态方式呈现活动控制器。'”

1 个答案:

答案 0 :(得分:4)

您的逻辑对我来说有点奇怪,您试图解除imagePickerController并再次呈现相同的视图控制器。错误消息基本上告诉你imagePickerController已经出现。

也许你可以将你的逻辑改为

if (![[self imagePickerController] isBeingDismissed])
{ [self dismissViewControllerAnimated:NO 
                          completion:^{
                               [self presentViewController:self.imagePickerController animated:NO completion:nil];
                           }];
}
else 
  [self presentViewController:self.imagePickerController animated:NO completion:nil];

不知道这是否正是您所寻找的。但希望能给你一些提示。