簡體   English   中英

NSTextView / NSViewController \\ NSNotification用於SWIFT中MacOS應用程序的控制台日志功能

[英]NSTextView/NSViewController\NSNotification used for a console log function for a MacOS app in SWIFT

我正在嘗試開發一個應用程序,其中包含一個帶有單個NSTextView的StoryBoard,我想用它來記錄應用程序事件和用戶反饋。 我從主視圖中的復選框開關加載視圖(默認情況下,當應用程序啟動時,控制台日志視圖處於打開狀態)。 所有這些都適用於這一點,應用程序啟動和第二個視圖使用viewDidLoad()函數預期的文本。

控制台日志視圖的NSViewController類是:

class ConsoleLogViewController: NSViewController,NSTextViewDelegate {

@IBOutlet var textViewConsoleLog: NSTextView!

override func viewWillDisappear() {
    (representedObject as! NSButton).isEnabled = true
    (representedObject as! NSButton).state = NSOffState
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

    textViewConsoleLog.delegate = self
    textViewConsoleLog.string = "All new journal events are logged to this window"
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("*********************************************")
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("\n")
} // END OF viewDidLoad()


func addLogToConsoleWindow(newLogEntry: String) {
    textViewConsoleLog.string? = (newLogEntry)
    textViewConsoleLog.string?.append("\n")
} // END OF addLogToConsoleWindow()

} // ENDLogLogViewController

使用以下函數調用從主視圖控制器(在下圖中命名為“Control”)啟動View:

    func showConsoleLogView() {
    let buttonCall = chkBoxConsoleLog
    performSegue(withIdentifier: "sequeConsoleView", sender: buttonCall)
}  //  END OF showConsoleLogView()

在主視圖控制器中,我創建了一個ConsoleLogViewController實例,然后嘗試使用addLogToConsoleWindow函數將其他日志信息附加到textViewConsoleLog。 主視圖控制器中相關的代碼片段是:

    let consoleOutputPrintStatement = ConsoleLogViewController()  // Create an instance of ConsoleLogViewController to allow logging to the new console window

consoleOutputPrintStatement.addLogToConsoleWindow(newLogEntry: "Loading journal files")

但是,當我運行應用程序並嘗試編寫其他文本行時,我會收到fatal error: unexpectedly found nil while unwrapping an Optional value 很明顯textViewConsoleLog對象是nil並且正在創建錯誤。

我假設問題是我正在創建該類的另一個實例,因此我試圖將附加文本附加到尚未啟動的textViewConsoleLog對象(或類似的東西!)。

該應用在首次運行時看起來像這樣:

在此輸入圖像描述

那么 - 我是否正試圖使用​​NSTextView向控制台開發日志記錄功能? 或者我是否關閉但需要使用NSTextView的不同方法才能使其正常工作? 如何使用從其他viewControllers生成的相關字符串更新NSTextField中的文本?

任何協助或指導高度贊賞。

好的 - 我最終使用NSNotification解決了一個解決方案 - 后見之明非常明顯。

控制台日志視圖中的相關代碼如下所示,正在設置類以使用addObserver方法從NotificationCenter接收通知:

class ConsoleLogViewController: NSViewController,NSTextViewDelegate {

@IBOutlet var textViewConsoleLog: NSTextView!

let controlCentreDidSendConsoleNotification = "TCCEDJ.controlCentreDidSendConsoleNotification"
let controlCentreDidSendConsoleNotificationMessageKey = "TCCEDJ.controlCentreDidSendConsoleNotificationMessageKey"


override func viewWillDisappear() {
    (representedObject as! NSButton).isEnabled = true
    (representedObject as! NSButton).state = NSOffState
}


override func viewDidLoad() {
    super.viewDidLoad()

    textViewConsoleLog.delegate = self
    textViewConsoleLog.string = "All new journal events are logged to this window"
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("*********************************************")
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("\n")

    //  set up notification centre
    let notificationCentre = NotificationCenter.default
    notificationCentre.addObserver(self, selector: #selector(addLogToConsoleWindow), name: NSNotification.Name(rawValue: controlCentreDidSendConsoleNotification), object: nil)
} // END OF viewDidLoad()


@objc func addLogToConsoleWindow(newLogEntry: NSNotification) {

    let userInfo = newLogEntry.userInfo as! [String: String]
    let newLogEntryString = userInfo[controlCentreDidSendConsoleNotificationMessageKey]!

    textViewConsoleLog.string?.append(newLogEntryString)
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.scrollRangeToVisible(NSMakeRange((textViewConsoleLog.string?.characters.count)! - 1, 0))
} // END OF addLogToConsoleWindow()

} // ENDLogLogViewController

主視圖控制器中的相關代碼是:

    // Set up variables to use with notification centre
let controlCentreDidSendConsoleNotification = "TCCEDJ.controlCentreDidSendConsoleNotification"
let controlCentreDidSendConsoleNotificationMessageKey = "TCCEDJ.controlCentreDidSendConsoleNotificationMessageKey"

    let testLog = [controlCentreDidSendConsoleNotificationMessageKey: "Test log on button push"]
    let notificationCentre = NotificationCenter.default
    notificationCentre.post(name: NSNotification.Name(rawValue: controlCentreDidSendConsoleNotification), object: self, userInfo: testLog)

所有現在都適用於我正在尋找的行為。

暫無
暫無

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

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