tags:

views:

494

answers:

3

I have a simple cocoa user interface with a list of items and a search field, implemented using NSTableView and NSSearchField, respectively. The data source and all of the bindings are set up and working nicely. I see my data in the list, and I can search through it by typing strings in the search field. As I type in more text, the number of items in the list gets smaller and smaller, eventually reduced to the one item I was searching for.

Now, how can I clear the text in the search field and force the list to go back to normal? I can make this happen by clearing the text manually (using the keyboard), but when I try to do it programmatically, the hidden items in the list do not come back.

I'm using this:

[searchField setStringValue:@""];

to clear the text in the search field, but it does not reset the list.

Any ideas? Is there a simple [searchField reset] method that I just can't find in the documentation?

+2  A: 

I figured it out. The following code works:

[searchField setStringValue:@""];
[[[searchField cell] cancelButtonCell] performClick:self];
e.James
I hate leaving questions "unanswered" just because I don't have the ability to mark my own answer as accepted. Can someone please re-post my answer as their own?
e.James
Ask and ye shall receive.
wfarr
+2  A: 

I figured it out. The following code works:

[searchField setStringValue:@""];
[[[searchField cell] cancelButtonCell] performClick:self];
wfarr
+1  A: 

[[[searchField cell] cancelButtonCell] performClick:self]; might work, but it really seems like there should be a "proper" solution. Is your table view actually bound to the searchField's value, or is it bound to some intermediate object that is not getting updated when you set the searchField's contents to the empty string programatically (but which is getting updated when you type because of the way the bindings are set up in the nib)?

erikprice
I agree, but I haven't been able to find anything. When I was googling for an answer, there were several other developers asking the same question.
e.James
I have both the search field and the tableview bound to an NSArrayController object which supplies the data.
e.James