NSTextView/NSViewController\n用于SWIFT中MacOS应用程序的控制台日志功能的通知
我正在尝试开发一个应用程序,其中包括一个带有单个NSTextView的故事板,我想用它来记录应用程序事件和用户反馈。我从主视图中的复选框开关加载视图(当应用程序启动时,控制台日志视图默认打开)。在应用程序启动和第二个视图使用viewDidLoad()函数中预期的文本时,所有这些都可以正常工作 控制台日志视图的NSViewController类为:NSTextView/NSViewController\n用于SWIFT中MacOS应用程序的控制台日志功能的通知,swift,macos,nstextview,nsviewcontroller,nsnotification,Swift,Macos,Nstextview,Nsviewcontroller,Nsnotification,我正在尝试开发一个应用程序,其中包括一个带有单个NSTextView的故事板,我想用它来记录应用程序事件和用户反馈。我从主视图中的复选框开关加载视图(当应用程序启动时,控制台日志视图默认打开)。在应用程序启动和第二个视图使用viewDidLoad()函数中预期的文本时,所有这些都可以正常工作 控制台日志视图的NSViewController类为: class ConsoleLogViewController: NSViewController,NSTextViewDelegate { @IBO
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
及
但是,当我运行应用程序并尝试写入额外的文本行时,我得到了一个致命错误:在展开可选值时意外发现nil
。很明显,textViewConsoleLog对象为nil,并且正在创建错误
我假设问题在于我正在创建该类的另一个实例,因此我试图向尚未启动的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"
及
现在,所有这些都与我所寻找的行为一致。好的-我最终使用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"
及
所有这些现在都与我正在寻找的行为一起工作。标题是关于
NSTextView
和日志记录。问题是关于NSViewController
。更改标题以吸引NSViewController
专家。我正在创建该类的另一个实例
呃,我之前错过了该部分。这很可能是问题的原因,是的。插座与这个新实例中的任何东西都没有关联。是的,Eric-但是,如何写入由segue创建的实例??作为一个新手,我很困惑/不确定如何处理这个问题。我自己不使用故事板,但这似乎适用于你的情况:在这个例子中,他们传递一个字符串-但这可能是任何东西。我想您也可以使用委托。标题是关于NSTextView
和日志记录。问题是关于NSViewController
。更改标题以吸引NSViewController
专家。我正在创建该类的另一个实例
呃,我之前错过了该部分。这很可能是问题的原因,是的。插座与这个新实例中的任何东西都没有关联。是的,Eric-但是,如何写入由segue创建的实例??作为一个新手,我很困惑/不确定如何处理这个问题。我自己不使用故事板,但这似乎适用于你的情况:在这个例子中,他们传递一个字符串-但这可能是任何东西。我想你也可以授权。