簡體   English   中英

在第二個 ViewController 中使用 NSDocument 保存

[英]Saving with NSDocument in a Second ViewController

我試圖簡單地使用 NSDocument 保存到 rtf。 代碼工作正常,但是當我嘗試保存到不是窗口控制器初始子級的視圖控制器時,它會從模式中拋出錯誤,提示“文檔“”無法保存為“”。

如何將文件保存到第二個視圖控制器?

             Window Controller
                    |
           Login View Controller

          |                |
SidebarViewContoller   ViewController1
                                 |
                                  TableViewController 2 Replaces VC1  
                                   Save TextView in this VC

我希望能夠將數據從 ViewController2 中的 textView 寫入我的 NSDocument 並將其保存到桌面就像你在 Pages 中所做的那樣

這是代碼

// 文檔.swift

class Document: NSDocument {

var text = NSAttributedString()
var documentViewController: DocumentViewController? {
    return windowControllers[0].contentViewController as? DocumentViewController
}


override init() {
    super.init()
    // Add your subclass-specific initialization here.
}

override class var autosavesInPlace: Bool {
    return true
}

override func makeWindowControllers() {
    // Returns the Storyboard that contains your Document window.
    let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
    let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
    self.addWindowController(windowController)
}

override func data(ofType typeName: String) throws -> Data {
    // Save the text view contents to disk
if let textView = documentViewController?.textView {
        let rangeLength = textView.string.count

    textView.breakUndoCoalescing()
    let textRange = NSRange(location: 0, length: rangeLength)
    if let contents = textView.rtf(from: textRange) {
            return contents
        }
    }
    throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}

override func read(from data: Data, ofType typeName: String) throws {
    if let contents = NSAttributedString(rtf: data, documentAttributes: nil) {
        text = contents
    }
}

//第二個視圖控制器

override func viewDidAppear() {
    let document = self.view.window?.windowController?.document as!   Document
    textView.textStorage?.setAttributedString(document.text)
}

您在應用程序中設置事物的方式,您將無法將整個 NSDocument 的內容放在文本視圖中。 假設您在表格視圖中有 10 個項目並且選擇一個項目會用一些文本填充文本視圖。 對於單個文檔中的 10 個表視圖項中的每一個,您將無法擁有一個 NSDocument。

您將要做的是創建一個表示文本文件的數據結構。 想想書中的一章或劇本中的一個場景。 在您的 NSDocument 子類中,您將擁有這些數據結構的數組。 當您保存文檔時,您會將其保存為文件包裝器,它是一個文件目錄,在 Finder 中看起來像單個文件。 表格視圖中的每個項目的文件包裝器中都會有一個文本文件。 請參閱以下文章以了解有關文件包裝器的更多信息:

在 Swift 中使用文件包裝器

現在你想知道的是當表格視圖選擇改變時如何填充文本視圖。 這對於 Mac 故事板來說很痛苦,因為表視圖的控制器和文本視圖的控制器是不同的場景。 在表視圖的視圖控制器中實現委托方法tableViewSelectionDidChange 使用parent屬性獲取拆分視圖控制器。 使用拆分視圖控制器來獲取文本視圖的控制器。 將選定的行傳遞給文本視圖的控制器,並使用它來獲取要在文本視圖中顯示的正確文本文件。

拆分視圖控制器應該存儲對 NSDocument 的引用。 使用parent屬性從文本視圖的控制器訪問拆分視圖控制器。 通過訪問拆分視圖控制器,您可以訪問文檔以使用與表視圖中所選項目對應的文本文件的內容填充文本視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM