Force a find in an NSTextView
Force a find in an NSTextView
- Subject: Force a find in an NSTextView
- From: Fritz Anderson <email@hidden>
- Date: Mon, 02 Mar 2015 11:13:33 -0600
Target OS X 10.10.
How does a view controller force an NSTextView to perform a search if it doesn’t own the text view?
My application is a debugging tool to break an attributed string into style runs and display the runs in a table. I’d like to double-click a row in the table, and select the corresponding text in the NSTextView that displays the string. Eventually, I’ll have to carry the character ranges in the table data and use those, but this _should_ have been easy as a first attempt, and now my blood is up.
The text view uses a find bar, the scroll view positions it at the top. The text view is not editable, so the Replace UI isn’t shown. The bar shows, hides, and functions as expected. The table and text are on different sides of a split-view controller; their window controller tells the table controller about the text view.
There’s no problem in responding to the double-click. The table’s controller fills the find pasteboard with the text content of the run; other applications show the run’s text in their search interfaces.
My current approach is this: (Swift)
@IBAction func tableDoubleClicked(sender: NSClickGestureRecognizer) {
loadFindPasteboard(sender) // App-specific, does what it says.
self.tag = 2
attributedTextView.performTextFinderAction(self)
}
-performTextFinderAction: is defined in NSResponder; NSTextFinder uses the sender’s tag to determine the verb. (Smelly, yes — it expects an NSControl or menu item, but duck-typing is not completely dead.) 2 is “find next.”
This works. Once. Thereafter, it’s erratic. The search field updates correctly, but the selection matches the previous search string. (Again, the find PB updates as expected in other apps.) Then the search field stops updating at all, but pulses the selection as if a find-next had been performed. Maybe the field doesn’t update, but the search result does. Using the find-panel interface has the same result.
Evidently I’m in some sort of cache race that may be reasonable given the expected use of NSTextFinder, but you’d hope a factored view controller of that name could be used to… find text. I don’t see a way to force the flush, or to force the text view’s finder to reload.
Ideas? Or should I give up and do the range highlighting I’ll have to do anyway eventually?
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden