我正在尝试开发一个应用程序,其中包含一个带有单个 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()
} // ConsoleLogViewController 结束
使用以下函数调用从主视图控制器(下图中名为“Control”)启动视图:
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 对象为零并且正在创建错误。
我认为问题在于我正在创建该类的另一个实例,因此我试图将其他文本附加到尚未启动的 textViewConsoleLog 对象(或类似的东西!)。
该应用程序首次运行时如下所示:
那么 - 我是否只是走错了路,试图使用 NSTextView 向控制台开发日志记录功能?或者我关闭但需要使用 NSTextView 的不同方法才能使其工作?如何使用从其他 viewController 生成的相关字符串更新 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()
} // ConsoleLogViewController 结束
主视图控制器中的相关代码是:
// 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)
所有现在都适用于我正在寻找的行为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句