2017-05-27 31 views
1

我正在尝试开发一款应用程序,该应用程序包含一个StoryBoard,其中包含一个用于记录应用程序事件和用户反馈的NSTextView。我从主视图中的复选框切换加载视图(控制台日志视图在应用程序启动时默认打开)。所有这些都可以在应用程序启动时正常工作,第二个视图可以像viewDidLoad()函数一样处理文本。NSTextView/NSViewController NSNotification用于SWIFT中的MacOS应用程序的控制台日志功能

为控制台日志视图的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

OF END

的视图是从主视图控制器(下图中命名为 '控制')启动使用以下函数调用:

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对象(或类似的东西!)。

该应用程序是这样的,当它第一次运行:

enter image description here

所以 - 我只是标题下错了路试图建立一个日志记录功能,使用NSTextView控制台?或者我关闭了,但需要与NSTextView不同的方法才能使它工作?如何使用其他viewControllers生成的相关字符串更新NSTextField中的文本?

任何援助或指导高度赞赏。

+0

标题是关于'NSTextView'和日志。问题是关于'NSViewController'。改变标题以吸引'NSViewController'专家。 – Willeke

+0

'我正在创建班级的另一个实例'呃,我错过了那个部分。这很可能是问题的原因,是的。在这个新实例中,网点与任何事物都没有关联。 – Moritz

+0

是Eric - 但是,我如何写入由segue创建的实例?作为一个新手,我很困惑/不确定如何解决这个问题。 – Wolfstar

回答

0

好的 - 我终于整理出了一个使用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) 

所有现在工作与行为我在寻找。

相关问题