当在独立的单视图应用程序中呈现时,以下代码可以很好地工作。
但是当我尝试在 UISplitViewController 上加载相同的代码时,应用程序崩溃了,但是这段代码在 iOS 13 应用程序中就像一个魅力。但在 iOS 14 中它会崩溃并发生以下异常:
Terminating app due to uncaught exception '**UIViewControllerHierarchyInconsistency',
reason: 'child view controller:<UIPrintPreviewViewController: 0x10394fa00>
should have parent view controller:<CXSTransactionSelectionViewController: 0x10389e400>
but actual parent is:<UIPrintPanelTableViewController: 0x103808200>'**
我正在使用的代码:
- (UIViewController *)printInteractionControllerParentViewController: (UIPrintInteractionController *)printInteractionController {
return [CXSCommon getParentController_iPhone];
}
- (void)Print
{
UIPrintInteractionController *controller = [UIPrintInteractionController
sharedPrintController];
if(!controller){
NSLog(@"Couldn't get shared UIPrintInteractionController!");
return;
}
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(!completed && error){
NSLog(@"FAILED! due to error in domain %@ with error code %ld", error.domain, (long)error.code);
}
else
{
NSLog(@"complete");
}
};
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"My Job";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
UIMarkupTextPrintFormatter *textPrint = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:[NSString stringWithFormat:@"<!DOCTYPE html><html><body><p><h1>Hello World</h1></p><p><h2>Hello World</h2></p><p><h3>Hello World</h3></p><p><h4>Hello World</h4></p><p><h5>Hello World</h5></p><p><h6>Hello World</h6></p></body></html>"]];
pic.printFormatter = textPrint;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// [controller presentFromBarButtonItem:self.addressButton animated:YES completionHandler:completionHandler];
[controller presentAnimated:YES completionHandler:completionHandler];
}
else {
[controller presentAnimated:YES completionHandler:completionHandler];
}
}