I'm developing an iPad app, and when I run it in the simulator and hit the share button (bar button item), I have got the following error message (I did not have that issue with the iPhone app). How can I fix it?
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7bfa3c90>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
My code is the following (the top 3 lines are in ViewDidLoad):
self.navigationItem.titleView = titleButton
let shareButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "ShareIcon"), style: UIBarButtonItemStyle.Plain, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton
}
func shareButtonPressed(sender: AnyObject) {
NSLog("shareButton pressed")
let stringtoshare: String = ""
//add current screen shot image to share
let imagetoshare = captureScreen()
let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToWeibo]
self.presentViewController(activityVC, animated: true, completion: { _ in })
}
func captureScreen() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}