0

Я работаю над приложением, которое представляет экран редактирования профиля. Затем с этого экрана я хочу иметь возможность представить еще один модальный контроллер представлений, UIImagePickerController. Тем не менее, я не могу сделать это и по-прежнему получать следующее сообщение об ошибке:iOS - Можно ли представить контроллер модального представления из модально представленного контроллера представления?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is UIImagePickerController: 0x13d077000.'

В моем приложении, на моем профиле экране, у меня есть верхняя кнопка элемент правого бара представляет собой экран «редактировать профиль» модально как так:

// MyProfileViewController.m 
// ... 

- (void)rightBarButtonItemTapped:(id)sender 
{ 
    EditMyProfileViewController *editMyProfileVC = [[EditMyProfileViewController alloc] init]; 
    editMyProfileVC.delegate = self; 
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:editMyProfileVC]; 
    navVC.navigationBar.translucent = NO; 
    [self presentViewController:navVC animated:YES completion:^{}]; 
} 

Тогда из в контексте модально представленного экрана «редактировать профиль», я хочу, чтобы иметь возможность модальна настоящий UIImagePickerController. Я попробовать сделать это так:

// EditMyProfileViewController.m 
// ... 

#pragma mark - UIActionSheetDelegate 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if ([actionSheet isEqual:self.profilePicActionSheet]) 
    { 
     if (buttonIndex == 0) // Camera 
     { 
      [self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
      [self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypeCamera]; 
     } 
     if (buttonIndex == 1) // Photo Library 
     { 
      [self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
      [self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     } 
     if (buttonIndex == 2) // Saved Photos Album 
     { 
      [self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
      [self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; 
     } 
    } 
} 

- (void)showPhotoPickerUsingSourceType:(UIImagePickerControllerSourceType)sourceType 
{ 
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 
    if (status != ALAuthorizationStatusAuthorized) 
    { 
     // ... 
    } 
    else // status == ALAuthorizationStatusAuthorized 
    { 
     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self]; 
     imagePicker.sourceType = sourceType; 
     imagePicker.delegate = self; 
     imagePicker.allowsEditing = YES; 
     if ([UIImagePickerController isSourceTypeAvailable:sourceType]) 
     { 
      [self presentViewController:imagePicker animated:YES completion:^{ 
       NSLog(@"imagePicker is on screen..."); // <-- App crashes before we get here 
      }]; 
     } 
     else 
     { 
      [Helper helperShowAlertWithTitle:@"Selected Source Type Not Available"]; 
     } 
    } 
} 

Любая помощь очень ценится. Благодарю.

+0

Что такое 'self' при попытке представить сборщика изображения? – rmaddy

+0

'self' - это модно представленный контроллер представлений,' EditMyProfileViewController'. 'EditMyProfileViewController' находится в' UINavigationController', и это то, что фактически представлено как показано в 'rightBarButtonItemTapped'. –

+0

@matt Я добавил больше подробностей кода. Надеюсь, это поможет. –

ответ

4

Изменить

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self]; 

в

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
+0

Большое вам спасибо! –

+0

Хороший улов. Я не прокрутил достаточно, чтобы заметить, что используется неправильный инициализатор. – rmaddy

Смежные вопросы

 Смежные вопросы