Warning message after actionSheet buttons are pres

2019-09-10 12:58发布

问题:

I am very new to iOS development and have encountered an error that I just can't seem to find a solution for. I have searched for solutions everywhere, but maybe it is my newness that is preventing me from seeing the problem.

The exact warning that is printed in the log is:

Attempt to dismiss from view controller <_UIAlertShimPresentingViewController: 0x7aaa4b90> while a presentation or dismiss is in progress!

It occurs right after I touch a button on the actionSheet.

Here is the code:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    if (buttonIndex == 0) {

        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:picker animated:YES completion:nil];
    }
    else if (buttonIndex == 1) {

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:nil];

    }
    else if (buttonIndex == 2) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        [self presentViewController:picker animated:YES completion:nil];

    }

} else {

    if (buttonIndex == 0) {

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;


        [self presentViewController:picker animated:NO completion:NULL];


    }
    else if (buttonIndex == 1) {

        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        [self presentViewController:picker animated:YES completion:NULL];


    }


}
}

The implementation of the actionSheet, I have the IBAction connected a toolbar button located an the .xib file.

   - (IBAction)addImage:(id)sender {

UIActionSheet *popUpSheet = [[UIActionSheet alloc]
                             initWithTitle:nil
                             delegate:self
                             cancelButtonTitle:nil
                             destructiveButtonTitle:nil
                             otherButtonTitles: nil];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    [popUpSheet addButtonWithTitle:@"Camera"];
    [popUpSheet addButtonWithTitle:@"Photo Library"];
    [popUpSheet addButtonWithTitle:@"Camera Roll"];

    [popUpSheet addButtonWithTitle:@"Cancel"];

    popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;

}   else {

    [popUpSheet addButtonWithTitle:@"Photo Library"];
    [popUpSheet addButtonWithTitle:@"Camera Roll"];

    [popUpSheet addButtonWithTitle:@"Cancel"];

    popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;

}

[popUpSheet showFromBarButtonItem: self.toolbarItems[0] animated:YES];  }

Everything has been delegated correctly from what I can tell:

DetailViewController.m

@interface DetailViewController () < UINavigationControllerDelegate, UIImagePickerControllerDelegate >

DetailViewController.h

@interface DetailViewController : UIViewController <UIActionSheetDelegate>

Any insight would be greatly appreciated and extremely helpful.

回答1:

Your code looks correct. Try using the:

actionSheet:didDismissWithButtonIndex:

method. It's sent to the delegate after the animation ends. Hope it helps.



回答2:

Finally figured out the problem. The compiler was giving me this warning because Apple recently combined UIActionsheet and UIAlert into one controller type called UIAlertController. Used the new UIAlertController and my problem was solved.

- (IBAction)addImage:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

UIAlertController *popUpSheet = [UIAlertController alertControllerWithTitle:nil message:@"Select your Choice" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *photoLibrary = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                            [self presentViewController:picker animated:YES completion:nil];

                            NSLog(@"Photo Library was touched");
                            [popUpSheet dismissViewControllerAnimated:YES completion: nil];
                         }];

UIAlertAction *recentPhotos = [UIAlertAction actionWithTitle:@"Camera Roll" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                            [self presentViewController:picker animated:YES completion:nil];

                            NSLog(@"Camera Roll was touched");
                            [popUpSheet dismissViewControllerAnimated:YES completion: nil];

                        }];

UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                            [self presentViewController:picker animated:YES completion:nil];

                            NSLog(@"Camera was touched");
                            [popUpSheet dismissViewControllerAnimated:YES completion:nil];
                        }];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                            [popUpSheet dismissViewControllerAnimated:YES completion:nil];
                        }];

[popUpSheet addAction:photoLibrary];
[popUpSheet addAction:recentPhotos];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addAction:camera];
}

[popUpSheet addAction:cancel];

[self presentViewController:popUpSheet animated:YES completion:nil];
}