5

我在这方面遇到了真正的麻烦。我不知道出了什么问题,但它吐了出来:

错误:尝试显示未设置打印源(项目/项目/格式化程序/渲染器)的打印选项

如果我将代码设置为打印 png 图像,而不是 NSString 的内容,则该代码有效。

这是我的代码:

- (IBAction)pushPrint:(id)sender {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter setDateFormat:@"dd/MM/YY     HH:mm:ss"];
    NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release]; 

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    NSString *path = [[NSBundle mainBundle] pathForResource:t[NSString stringWithFormat:@"Results Printout\n"
                                                              "\n"
                                                              "%@\n"
                                                              "\n"
                                                              "\n"
                                                              "%@", currentTime, pasteboard.string] ofType:nil];

    NSData *myData = [NSData dataWithContentsOfFile:path];
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

    print.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = [path lastPathComponent];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = myData;

    void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"houston we have a problem");
        }
    };
    [print presentAnimated:YES completionHandler:completionHandler];
}
4

1 回答 1

8

您似乎想要打印一个 plain NSString,但您试图加载一个文件路径无效的文件。因此,myData很有可能nil(请与调试器确认),因此错误消息是正确的(print.printingItem一定不是nil)。

要打印简单文本,请创建UISimpleTextPrintFormatter(with initWithText:) 的实例并将该实例分配给print.printFormatter.

于 2011-02-21T17:59:59.193 回答