gpt4 book ai didi

swift - nsprintoperation - 检查用户选择了什么

转载 作者:行者123 更新时间:2023-11-30 12:17:05 25 4
gpt4 key购买 nike

使用此代码我可以打印 pdf 文件:

let dictDocuments = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let completePath = dictDocuments.appendingPathComponent("test.pdf")
let pdfDocument = PDFDocument(url: completePath!)

let printInfoDict = NSMutableDictionary(dictionary: NSPrintInfo.shared().dictionary())
printInfoDict.setObject(NSPrintSpoolJob, forKey: NSPrintJobDisposition as NSCopying)
let printInfo = NSPrintInfo(dictionary: printInfoDict as! [String : Any])
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
printInfo.scalingFactor = 1.5


let printOp = pdfDocument!.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)!
printOp.runModal(for: self.view.window!, delegate: self, didRun: nil, contextInfo: nil)

我可以检查用户在打印面板中选择了哪个按钮吗? (取消或打印)

最佳答案

对于 swift 5.1:

// prepare printOp
...

{
//was: printOp.run()

if let w = NSApp.mainWindow{
let selector = #selector(printOperationDidRun(printOperation: success: contextInfo:))
printOp.runModal(for: w, delegate: self,
didRun: selector,
contextInfo: nil)
}
}

// apple docs:
// - (void)printOperationDidRun:(NSPrintOperation *)printOperation success:(BOOL)success contextInfo:(void *)contextInfo

@objc func printOperationDidRun( printOperation: NSPrintOperation,
success: Bool,
contextInfo: UnsafeMutableRawPointer?){
// use callback..

}

关于swift - nsprintoperation - 检查用户选择了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com