簡體   English   中英

未調用dismissViewController完成處理程序

[英]dismissViewController completion handler not called

嘗試使用鏈接中第一個答案中的代碼(由Kampai提供): 如何使用UIAlertController替換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調用此方法以在調用操作處理程序代碼之前將其自身關閉。

在動作處理程序中,您只需要執行選擇該動作時應執行的所有操作:

在這種情況下:

  • 在取消操作中,您無需執行任何操作
  • 在“拍照”操作中,您拍照

同樣,從用戶體驗的角度來看,最好是在選擇“拍照”后立即禁用“拍照”或顯示警報,而不是在嘗試拍照后發出警報。 換句話說,指出問題的時間要早​​於而不是晚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM