이 (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];
UIAlertController를 닫으려면'dismissViewController : animated :'라고 하시겠습니까? 그렇다면 그렇게하지 마십시오. 단추를 누를 때 자동으로 해제됩니다. 액션 핸들러에서 액션 버튼이 선택되었을 때 실행하고자하는 코드를 작성하면됩니다. 예 : "취소"작업이 비어 있습니다. – Paulw11
고마워요! 나는 또한 그것을 발견했다. 그냥 궁금해서, 언제 사용해야합니까? "[self dismissViewControllerAnimated : 예 완료 :^{ }];"? 이 완료 처리기가 작동하지 않는 이유는 무엇입니까? –
보기 컨트롤러를 닫을 때마다이 옵션을 사용합니다. 여기에서 사용하지 않는 이유는'UIAlertController'가 이미 해제되었다는 것입니다. 사용자가 옵션을 선택하지 않은 경우 경고가 5 초 후에 취소 된 시간 제한을 원한다고 가정 해보십시오. 당신은 타이머를 시작할 수 있고 5 초 후에'dismissViewController : animated :'를 호출하여 경고를 제거 할 수 있습니다. – Paulw11