2017-05-27 7 views
1

アプリイベントとユーザーのフィードバックを記録するために使用する単一のNSTextViewを含むStoryBoardを含むアプリケーションを開発しようとしています。メインビューからチェックボックススイッチからビューをロードします(コンソールログビューは、アプリケーションの起動時にデフォルトでオンになっています)。アプリケーションの起動とviewDidLoad()関数から期待どおりのテキストを扱う2番目のビューで、この時点までのすべての作業がうまく動作します。SWIFTのMacOSアプリケーションのコンソールログ機能にNSTextView/NSViewController NSNotificationが使用されました

コンソール・ログ・ビューのNSViewControllerクラスがある:ConsoleLogViewController

OF

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() 

} // 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オブジェクトがnilであり、エラーを作成していることは明らかです。

私は、クラスの別のインスタンスを作成しているため、開始されていないtextViewConsoleLogオブジェクトに追加のテキストを追加しようとしているとします。私はちょうどNSTextViewを使用して、コンソールへのロギング機能を開発しようとして間違った道を見出しています -

enter image description here

だから:それは最初の実行時に

アプリはこのようになりますか?または、私は閉じるが、それを動作させるためにNSTextViewと異なるアプローチが必要なのだろうか?他のviewControllersから生成された関連する文字列でNSTextFieldのテキストを更新するにはどうすればよいですか?

何か助けや指導をいただければ幸いです。

+0

タイトルは 'NSTextView'とロギングについてです。問題は 'NSViewController'です。 'NSViewController'エキスパートを引き付けるためにタイトルを変更してください。 – Willeke

+0

'私はクラスの別のインスタンスを作成しています。えー、以前はその部分を逃しました。これはおそらく問題の原因です。はい。アウトレットは、この新しいインスタンスのものと関連付けられていません。 – Moritz

+0

はいEric - ただし、segueによって作成されたインスタンスにどのように書き込むのですか?私は初心者としてこれにアプローチする方法を混乱/不確かです。 – Wolfstar

答えて

0

OK - 最終的には、NSNotificationを使用して解決策をソートしました。

コンソールログビューの関連するコードは、次のとおりです。クラスは、addObserverメソッドを使用してNotificationCenterから通知を受け取るように設定されています。:ConsoleLogViewController

メインビュー・コントローラに関連するコード

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() 

} // ENDである:今その挙動で動作

// 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) 

すべて私は探していた。

関連する問題