尝试使用链接中第一个答案中的代码(由 Kampai 提供): How to use UIAlertController to replace UIActionSheet?
但是,我的代码中甚至没有调用完成处理程序。
按下两个按钮后可以关闭警报操作表,但完成处理程序内部没有任何作用。 知道可能是什么问题吗?我是使用完成处理程序的新手,并试图在网上找到答案,但很少有人遇到与我相同的问题。
- (IBAction)takePhotoButtonPressedUIButton *)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];
您无需在操作处理程序中调用 dismissViewController:animated:
即可移除警报。 UIAlertController
在调用 Action 处理程序代码之前调用它来关闭自身。
在您的操作处理程序中,您只需执行选择该操作时应该执行的任何操作:
在这种情况下:
此外,从用户体验的角度来看,最好禁用“拍照”或在他们选择后立即显示警报,而不是在他们尝试拍照后发出警报;换句话说,早点而不是晚点指出问题
关于ios - 未调用dismissViewController 完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908826/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |