Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionSheet not working iPad

I'm using ActionSheet in my application. On my iPhone it works, but it doesn't on the iPad simulator.

this is my code:

@IBAction func dialog(sender: AnyObject) {      let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)     let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: {          (alert: UIAlertAction!) -> Void in         println("Filtre Deleted")     })      let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {         (alert: UIAlertAction!) -> Void in         println("Cancelled")     })      optionMenu.addAction(deleteAction)     optionMenu.addAction(cancelAction)      self.presentViewController(optionMenu, animated: true, completion: nil) } 

And my error:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () 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's 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.'

like image 965
Stephany Avatar asked Jan 22 '15 13:01

Stephany


1 Answers

You need to provide a source view or button just before presenting optionMenu since on iPad its a UIPopoverPresentationController, As it says in your error. This just means that your action sheet points to the button letting the user know where it started from.

For example if you're presenting your optionMenu by tapping on the right navigation bar item. You could do something like this:

optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem  self.presentViewController(optionMenu, animated: true, completion: nil) 

or you could set a view like this:(You just need one of these 2)

optionMenu.popoverPresentationController?.sourceView = yourView  self.presentViewController(optionMenu, animated: true, completion: nil) 

Also keep in mind that if you change your UIAlertControllerStyle to Alert instead of action sheet, You wouldn't need to specify this. I am sure you must have figured it out but i just wanted to help anyone who comes across this page.

like image 135
MD Singh Avatar answered Nov 15 '22 14:11

MD Singh