gpt4 book ai didi

ios - 未调用 dismissViewController 完成处理程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:38:16 26 4
gpt4 key购买 nike

尝试使用链接中第一个答案中的代码(来自 Kampai): How to use UIAlertController to replace UIActionSheet?

但是,我的代码中甚至没有调用完成处理程序。

按下两个按钮后可以关闭警报操作表,但完成处理程序中的任何内容都不起作用。知道可能是什么问题吗?我刚开始使用完成处理程序并尝试在网上找到答案,但很少有人遇到与我相同的问题。

- (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 关闭 ViewController。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 在调用操作处理程序代码之前调用它来关闭自身。

在您的操作处理程序中,您只需执行选择该操作时应该执行的任何操作:

在这种情况下:

  • 在您的取消操作中,您不需要做任何事情
  • 在您的“拍照”操作中您拍照

此外,从用户体验的角度来看,最好禁用“拍照”功能或在他们选择后立即显示警报,而不是在他们尝试拍照后发出警报;换句话说,更早而不是更晚地指出问题

关于ios - 未调用 dismissViewController 完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com