UIPrintInteractionController PresentFromRect 问题
我使用presentFromRect方法从UIButton显示AirPrint选项,一切都按预期工作,但如果我继续按足够快的按钮,我的应用程序会因EXC_BAD_ACCESS而崩溃,可能是因为弹出窗口没有被释放。
我使用的是启用了 ARC 的 Xcode 4.2。
任何帮助都会很棒!
更新2:该问题仅出现在iOS 5模拟器上,iPad 4.3模拟器按预期工作。
更新:
这里是真正的问题:
* 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“-[UIPopoverController dealloc] 在弹出窗口仍然可见时到达。”
这是代码:
这里我正在调用我的方法:
PrintUtils* printUtils = [[PrintUtils alloc] init];
printUtils.delegate = self;
[printUtils setHeader:@"Header"];
[printUtils print:self.webView fromRect:self.myButton.frame inView:self.menuView];
我的方法:
- (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];
}
我的尝试修复实现 UIPrintInteractionControllerDelegate 方法的问题:
- (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
{
if (visible) {
[printController dismissAnimated:YES];
}
}
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有没有弄清楚这一点?问题是您没有检查设备是 iPad 还是 iPhone。 iPhone 不支持presentFromRect:,但iPad 支持。这就是为什么当你在 iPhone 上运行它时会出现错误的访问。
您需要执行以下操作:
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: