1

이 (Kampai에 의해) 링크의 첫 번째 대답에서 코드를 사용하려고 호출되지 : How to use UIAlertController to replace UIActionSheet?dismissViewController 완료 핸들러는

그러나, 완료 핸들러가도 내 코드에서 호출되지 않습니다.

경고 조치 시트는 두 버튼을 모두 누르면 해제 될 수 있지만 완료 핸들러에서는 작동하지 않습니다. 무엇이 문제 일 수 있습니까? 완성 처리기를 처음 사용하고 온라인에서 답변을 찾으려고했지만 거의 동일한 문제가 없습니다.

- (IBAction)takePhotoButtonPressed:(UIButton *)sender { 
pressedButtonTagNumber = sender.tag; 

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 

    // Cancel button tappped 
    [self dismissViewControllerAnimated:YES completion:^{ 
    }]; 
}]]; 

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Take a Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    NSLog(@"!"); 
    // Take a Photo button tapped 
    [self dismissViewControllerAnimated:YES completion:^{ 
     NSLog(@"0"); // NOT CALLED 
     // Initialize UIImagePickerController 
     UIImagePickerController *takePhotoImagePickerController = [[UIImagePickerController alloc] init];   takePhotoImagePickerController.delegate = self; 
     takePhotoImagePickerController.allowsEditing = YES; 
     NSLog(@"1"); 
     // Check and assign image source 
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
      NSLog(@"2"); 
      UIAlertController *noCameraErrorSheet = [UIAlertController alertControllerWithTitle:@"Camera is not available" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 
      [noCameraErrorSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 
       // Cancel button tappped 
       [self dismissViewControllerAnimated:YES completion:^{ 
       }]; 
      }]]; 
     } else { 
      takePhotoImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
      // Present UIImagePickerController 
      [self presentViewController:takePhotoImagePickerController animated:YES completion:NULL]; 
     } 

    }]; 
}]]; 

솔루션 : 솔루션은 잘 작동

Paulw11 @ :

1) UIAlertController에 대한 dismissViewController 필요가 없습니다. 2) 새로운 UIAlertController를 래핑하는 경우에는이를 호출 할 수 없습니다 (분명히). 3) 미리 버튼을 확인하고 비활성화하는 것이 좋습니다.

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 
}]]; 

UIAlertAction *takePhotoActionButton = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [self takePhoto]; 
}]; 
UIAlertAction *uploadPhotoActionButton = [UIAlertAction actionWithTitle:@"Upload from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [self uploadPhoto]; 
}]; 

// Disable take a photo button if source not available 
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
    [takePhotoActionButton setEnabled:FALSE]; 
} 
// Disable upload a photo button if source not available 
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
    [uploadPhotoActionButton setEnabled:FALSE]; 
} 

[actionSheet addAction:takePhotoActionButton]; 
[actionSheet addAction:uploadPhotoActionButton]; 

// Present action sheet. 
[self presentViewController:actionSheet animated:YES completion:nil]; 
+2

UIAlertController를 닫으려면'dismissViewController : animated :'라고 하시겠습니까? 그렇다면 그렇게하지 마십시오. 단추를 누를 때 자동으로 해제됩니다. 액션 핸들러에서 액션 버튼이 선택되었을 때 실행하고자하는 코드를 작성하면됩니다. 예 : "취소"작업이 비어 있습니다. – Paulw11

+0

고마워요! 나는 또한 그것을 발견했다. 그냥 궁금해서, 언제 사용해야합니까? "[self dismissViewControllerAnimated : 예 완료 :^{ }];"? 이 완료 처리기가 작동하지 않는 이유는 무엇입니까? –

+0

보기 컨트롤러를 닫을 때마다이 옵션을 사용합니다. 여기에서 사용하지 않는 이유는'UIAlertController'가 이미 해제되었다는 것입니다. 사용자가 옵션을 선택하지 않은 경우 경고가 5 초 후에 취소 된 시간 제한을 원한다고 가정 해보십시오. 당신은 타이머를 시작할 수 있고 5 초 후에'dismissViewController : animated :'를 호출하여 경고를 제거 할 수 있습니다. – Paulw11

답변

3

당신은 경고를 제거하는 작업 핸들러 내에서 dismissViewController:animated:를 호출 할 필요가 없습니다. UIAlertController은 액션 핸들러 코드를 호출하기 전에 이것을 호출하기 위해 이것을 호출한다. 이 경우

:

  • 당신의 취소 소송에서 당신은 아무것도
  • 을 수행 할 필요가 없습니다 액션 핸들러에서

    당신은 단순히 해당 작업을 선택했을 때의 어떤 실행해야 당신의 "사진을 찍을"조치에

  • 당신이보기의 사용자 경험의 관점에서,

또한 사진을 촬영, 그것은 "이 사진을"사용하지 않도록하는 것이 더있을 수 있습니다 또는 그들이 사진을 찍으려고 한 후에 경고를 내기보다는 그것을 선택하자마자 경고를 제시하십시오. 다시 말하면 문제를 조기에 알려 줌

+0

사용자에게 경고하는 또 다른 질문입니다. 이제는 항상 "할당 해제되는 동안보기 컨트롤러보기를로드하려고 시도하는 것이 허용되지 않으며 정의되지 않은 동작이 발생할 수 있습니다 ()"라는 예외가 있습니다. 현재 UIAlertController를 다른 UIAlertController에 넣기 때문입니까? 나는 약간의 "누가"오류 메시지에 언급 된대로 할당을 혼란 스러워요. 온라인으로 검색하여 상위 컨트롤러 등에서 UIAlertController를 제거하려고 시도했지만 아무 것도 작동하지 않습니다. –

+0

예, 두 번째 경고 컨트롤러를 첫 번째 경고 컨트롤러에 표시하려고 시도하지만 첫 번째 경고 컨트롤러가 닫히기 때문에 그렇습니다. 앞에서 말한 것처럼 첫 번째 경고를 표시하기 전에 카메라가 사용 가능한지 확인해야합니다. – Paulw11

+0

솔루션이 좋습니다. 모든 작품은 지금. –