0

I am using UIImagePickerController in my app and it crashes whenever I click on cancel for 2nd time and shows following crash log :

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller: should have parent view controller:(null) but actual parent is:'

Dismissing code :

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

The same code is working fine in other app but when I copied the code into new project the crash is occurring repeatedly. Its happening on iOS 8.3 & 7.1, not tested on other versions. Any help would be much appreciable.

Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33

1 Answers1

0

Set delegate in .h

@interface YourViewController : UIViewController < UIImagePickerControllerDelegate, UINavigationControllerDelegate>

Initialize image picker when you want to open it.

UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.allowsEditing = YES;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imgPicker animated:YES completion:^{ }];

Delegate method

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • Thanks a lot, you saved lot of my time. But the real issue is imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen; here I was using UIModalPresentationCurrentContext and it was causing the crash in my new app but not in previous app. – Paras Gorasiya Jul 09 '15 at 10:01