How to fix runtime error using UIAlertController

I have this code in the UIVIewController (Xcode 6.1, iOS 8.1.1):

[UIAlertController showActionSheetInViewController:self withTitle:@"Test Action Sheet" message:NSLocalizedString(@"Are you sure you want to delete ALL appointments?",nil) cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Yes" otherButtonTitles:@[@"No"] // same as Cancel tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){ if (buttonIndex == UIAlertControllerBlocksCancelButtonIndex) { NSLog(@"Cancel Tapped"); } else if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) { NSLog(@"Delete Tapped"); } else if (buttonIndex >= UIAlertControllerBlocksFirstOtherButtonIndex) { NSLog(@"Other Action Index %ld", (long)buttonIndex - UIAlertControllerBlocksFirstOtherButtonIndex); } }]; 

When I run it, I get this error at runtime:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fdfe3324f00>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

What do I need to do to make this work? (I looked at SO and Google and did not find anything specific). I appreciate any help I can get from this ...

UPDATE I rewrote it without third-party code; this code is added and now it works!

  UIAlertController * view= [UIAlertController alertControllerWithTitle:@"My Title" message:@"Select your Choice" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Do some thing here [view dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [view dismissViewControllerAnimated:YES completion:nil]; }]; [view addAction:ok]; [view addAction:cancel]; view.popoverPresentationController.sourceView = self.view; view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0); [self presentViewController: view animated:YES completion:nil]; 
+6
source share
2 answers

There is very little information ...

It looks like you are using https://github.com/ryanmaxwell/UIAlertController-Blocks and not the standard UIAlertController, in which case the exception suggests changes that the version of code you use until it covers or uses a use case that requires additional work with your side.

I have never used this third-party code, but in the quick check you can’t see any obvious β€œdo it” in the documents. My initial recommendation was to implement the delegate method in the view in question and give it what it wants - the place where the popover will be presented.

+1
source

The error message you received appeared because you ran the iPhone code on the iPad. To use it on an iPad, you need to set alertController popoverPresentationController. The source rectangle can be generated without calculating the sloppy size. Below is a complete method showing how you come across the code when a button is clicked. After setting up the AlertController the way you want, you will get your popoverPresentationController and configure it for use with the iPad. In the method below, the button that was pressed is the sender. Therefore, we send the sender back to this button, and then use the button to set the rectangle. No random measurements require calculation. Now, if you run the code on the iPad, popover will not display the Cancel button (which appears on the iPhone). This is by design. If you look at the Apple UIPopoverController documentation, you will see that popover is canceled by selecting it.

 - (IBAction)showImagePickerButtonTapped:(id)sender; { BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; if (isCameraAvailable) { [alertController addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera]; }]]; } if (isPhotoLibraryAvailable) { [alertController addAction:[UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; }]]; } // The following lines are needed for use with the iPad. UIPopoverPresentationController *alertPopoverPresentationController = alertController.popoverPresentationController; UIButton *imagePickerButton = (UIButton*)sender; alertPopoverPresentationController.sourceRect = imagePickerButton.frame; alertPopoverPresentationController.sourceView = self.view; [self showDetailViewController:alertController sender:sender]; } 
+7
source

Source: https://habr.com/ru/post/980239/


All Articles