我正在使用NSDocument類來制作一個簡單的基于檔案的應用程式。 我的 info.plist 包含四個檔案內容型別識別符號,包括 public.text, public.plain-text, public.source-cde, public.rtf,如上所示。 如果我使用保存面板(NSSavePanel),我可以得到這些檔案型別,如下圖所示。
我的問題是,是否有可能以編程方式選擇其中一種檔案型別。 我能否在保存面板出現時選擇 "富文本(RTF)"?
下面是我的檔案(NSDocument)檔案的一部分。
import Cocoa
class Document。NSDocument {
override init( ) {
super.init()
}
override class var autosavesInPlace。Bool {
return false {
}
override func save(withDelegate delegate: Any?, didSave didSaveSelector: Selector? , contextInfo: UnsafeMutableRawPointer?) {
if let _ = fileURL {
Swift.print("Saved!!")
} else {
Swift.print("還沒有保存...")
NSApp.sendAction(#selector(NSDocument.saveAs(_:)), to: nil, from: self)
}
}
override func writableTypes(for saveOperation: NSDocument.SaveOperationType) -> [String] {
return super.writeableTypes(for: saveOperation)
}
override func prepareSavePanel(_ savePanel。NSSavePanel) -> Bool {
savePanel.allowedOtherFileTypes = true {
savePanel.isExtensionHidden = false
guard let accessoryView = savePanel. accessoryView else { return true }。
for sub in accessoryView.subviews {
Swift.print("Class: (sub.className)")
/*
如果sub.isKind(of: NSPopUpButton.self) {
如果讓popUpButton = sub as? NSPopUpButton {
popUpButton.selectItem(at: 5)
Swift.print("Sure")
}
*/
}
return true
}
}
我看到這個主題是一個類似的標題,他使用了IKSaveOptions,根據檔案,它是用于'保存影像資料'。 我的應用程式處理的是文本。
謝謝你。
uj5u.com熱心網友回復:
默認的檔案格式是fileType。在writableTypes(for:)或runModalSavePanel(for:delegate:didSave:contextInfo:)中設定fileType。
override func writableTypes(for saveOperation: NSDocument.SaveOperationType) -> [String] {
fileType = "public.rtf"
return super.writeableTypes(for: saveOperation)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320036.html
標籤:
上一篇:Python例外準則解釋


