CodexBloom - Programming Q&A Platform

NSTextView Not Updating Text After Paste in macOS 14 - Focus Issues

👀 Views: 95 💬 Answers: 1 📅 Created: 2025-09-01
macos NSTextView Swift

I'm testing a new approach and I'm updating my dependencies and I'm migrating some code and I'm sure I'm missing something obvious here, but I'm facing a strange issue with an `NSTextView` in my macOS application running on macOS 14. When I paste text into the text view, the content doesn't update visually, even though the text is actually pasted. The cursor blinks as if it's accepting input, but the view does not reflect the pasted text until I manually click into it or change focus. I've tried calling `setNeedsDisplay:` on the text view after the paste operation, but that didn’t resolve it. Here's the code snippet I’m using to handle the paste action: ```swift class MyViewController: NSViewController { @IBOutlet weak var myTextView: NSTextView! @IBAction func pasteText(_ sender: Any) { NSPasteboard.general.pasteboardChangedNotification.addObserver(self, selector: #selector(pasteTextFromClipboard), name: .NSPasteboardDidChange, object: nil) myTextView.paste(nil) // Attempt to force a refresh myTextView.setNeedsDisplay(true) } @objc func pasteTextFromClipboard() { // This is called when the pasteboard changes print("Pasted text: \(myTextView.string)") } } ``` The only time the text view properly updates is when I interact with it again, which is annoying for user experience. I've checked my constraints and autolayout settings, and everything looks fine. Also, I've ensured that the pasteboard contains valid data before calling `myTextView.paste(nil)`, but there seem to be no warnings or errors in the console. Has anyone encountered similar behavior or have suggestions on how to ensure the `NSTextView` updates properly after a paste operation? Any insights on this would be greatly appreciated! For context: I'm using Swift on Windows. What am I doing wrong? The project is a mobile app built with Swift. How would you solve this? I'm using Swift 3.10 in this project. I'd really appreciate any guidance on this. For context: I'm using Swift on Linux.