Ios 未调用dismissViewController完成处理程序
尝试使用链接中第一个答案的代码(由Kampai提供): 但是,在我的代码中甚至没有调用完成处理程序 按下两个按钮后,可以解除警报操作表,但完成处理程序中的任何内容都不起作用。 知道有什么问题吗?我不熟悉使用完成处理程序,并尝试在网上找到答案,但很少有人有和我一样的问题Ios 未调用dismissViewController完成处理程序,ios,objective-c,uiviewcontroller,completionhandler,ios-camera,Ios,Objective C,Uiviewcontroller,Completionhandler,Ios Camera,尝试使用链接中第一个答案的代码(由Kampai提供): 但是,在我的代码中甚至没有调用完成处理程序 按下两个按钮后,可以解除警报操作表,但完成处理程序中的任何内容都不起作用。 知道有什么问题吗?我不熟悉使用完成处理程序,并尝试在网上找到答案,但很少有人有和我一样的问题 - (IBAction)takePhotoButtonPressed:(UIButton *)sender { pressedButtonTagNumber = sender.tag; UIAlertController *a
- (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解除IssViewController。
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
在调用操作处理程序代码之前调用此函数以解除其自身
在操作处理程序中,您只需执行选择该操作时应执行的任何操作:
在这种情况下:
- 在取消操作中,您不需要做任何事情
- 在你的“拍照”动作中,你要拍照
此外,从用户体验的角度来看,最好禁用“拍照”或在用户选择后立即显示警报,而不是在用户尝试拍照后发出警报;换句话说,提前指出问题,而不是晚些时候是否调用
dismissViewController:animated:
解除UIAlertController?如果是这样,就不要这样做。当点击按钮时,它会自动关闭。在操作处理程序中,您只需在选择操作按钮时编写想要执行的任何代码。例如,您的“取消”操作将是空库存!我也刚刚发现。只是想知道,我应该在什么时候使用“[self dismissViewControllerAnimated:YES completion:^{};”?为什么此完成处理程序不起作用?每当您想关闭视图控制器时,都会使用它。您在这里不使用它的原因是,UIAlertController
已经解除了自身功能。假设您想要一个超时,如果用户没有选择选项,警报在5秒后被取消;您可以启动计时器,并在5秒钟后调用dismissViewController:animated:
,以删除警报。@Paulw11解释得非常清楚。关于提醒用户的另一个问题。现在我总是遇到一个例外,即“不允许在视图控制器解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为()”。是因为我在当前控制器中放置了另一个UIAlertController吗?我对错误消息中提到的“谁”正在解除分配有点困惑。已联机搜索并尝试从父控制器中删除UIAlertController等,但现在一切都不起作用。是的,因为您将尝试在第一个控制器上显示第二个警报控制器,但第一个控制器将被取消。正如我所说,在显示第一个警报之前,您应该检查摄像头是否可用。