In a macOS app I am attempting to have a sheet popup where the user can input text. Once the user add's text they click save closing the sheet and adding the text to the previous pages NSTextView. I have tried many variations of this but I'm honestly not even sure how to pass data between two ViewControllers. I have tried PerformSegue which appears to be iOS only. I've also tried saving to a UserDefaults().string(forKey:"") but nothing seems to work. Finally I have come across a way to reference the other ViewController class and then run a function from that class but it doesn't appear to be working. Here is the code I have so far(I tried to add all the relevant code if something is missing let me know).
This is the ViewContoller connected to the View I want to add the text to.
class NotesViewController: NSViewController {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
@IBOutlet var notesTextView: NSTextView!
func updateCaseTemplate(template: Any) {
notesTextView.textStorage?.mutableString.setString(template as! String)
}
This is the ViewController where I am trying to get the users text.
class TemplateViewController: NSViewController {
private var notesView: NotesViewController?
@IBOutlet weak var notesTemplateTextView: NSTextView!
@IBAction func saveNotesTemplate(_ sender: Any) {
var notesTemplate = notesTemplateTextView.textStorage?.mutableString
notesView?.updateCaseTemplate(template: notesTemplate!)
dismissViewController(self)
}
}
I am able to run a print(notesTemplate!) in saveNotesTemplate and it is showing the correct text from the NSTextView!. If I put a Break on the func updateCaseTemplate() it never gets to it. Any help would be greatly appreciated. I am really trying to learn here and I have google'd everything I could trying to find a solution to this.
Finally, if am doing this completely wrong and there is a better way please let me know.
If you want to pass data between view controllers, you need to have a variable in the second view controller that can take what you want to pass. Then, you need to send it when you prepare to segue.
For example, This shows passing data from firstVC to secondVC when goToNext is tapped (which I've made a navigation button here)
FirstVC
SecondVC