问题
Im showing the AirPrint options from a UIButton using the presentFromRect method, everything is working as expected, but if i keep pressing the button fast enough my app crashes with EXC_BAD_ACCESS, probably because of the popover not being release.
Im using Xcode 4.2 with ARC enabled.
Any help would be great!
Update 2: The problem only occur on iOS 5 simulator, iPad 4.3 simulator works as expected.
Update:
Here is the real problem:
* Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.'
Here is the code:
Here I'm calling my method:
PrintUtils* printUtils = [[PrintUtils alloc] init];
printUtils.delegate = self;
[printUtils setHeader:@"Header"];
[printUtils print:self.webView fromRect:self.myButton.frame inView:self.menuView];
My method:
- (void)print:(UIWebView *)webView fromRect:(CGRect)rect inView:(UIView *)view
{
printController = [UIPrintInteractionController sharedPrintController];
[printController setDelegate:self];
if(!printController){
NSError* error = [[NSError alloc] initWithDomain:@"Print unavailable!" code:0 userInfo:nil];
[self showError:error];
return;
}
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(!completed && error){
[self showError:error];
}
};
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
[printInfo setJobName:header];
[printInfo setDuplex:UIPrintInfoDuplexLongEdge];
[printInfo setOutputType:UIPrintInfoOutputGeneral];
UIPrintFormatter* viewFormatter = [webView viewPrintFormatter];
CustomPrintPageRenderer *pageRenderer = [[CustomPrintPageRenderer alloc] init];
[pageRenderer setJobTitle:[printInfo jobName]];
UIFont* font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
CGSize titleSize = [pageRenderer.jobTitle sizeWithFont:font];
pageRenderer.headerHeight = pageRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
[pageRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
[printController setPrintPageRenderer:pageRenderer];
[printController setPrintInfo:printInfo];
[printController setPrintFormatter:viewFormatter];
[printController setShowsPageRange:YES];
[printController presentFromRect:rect inView:view animated:YES completionHandler:completionHandler];
}
My attempt to fix the problem implementing the UIPrintInteractionControllerDelegate Method:
- (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
{
if (visible) {
[printController dismissAnimated:YES];
}
}
回答1:
Did you ever figure this out? The problem is that you´re not checking if the device is an iPad or iPhone. iPhone does not support presentFromRect: but iPad does. That's why you get the bad access when you run it on iPhone.
Here's what you need to do:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[printController presentFromFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
} else {
[printController presentAnimated:YES completionHandler:completionHandler];
}
来源:https://stackoverflow.com/questions/8025399/uiprintinteractioncontroller-presentfromrect-issue