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

相關問題