tags:

views:

48

answers:

0

I have a NSCollectionView (MyCollectionView) on main window, each collection view item (MyCollectionViewItem) contains a text field inside it, so basically it looks like this:

|---------MyCollectionView---------|  
|                                  |  
|  |----------------------------|  |  
|  |textfield                   |  |  
|  |----------------------------|  |  
|                                  |
|  |----------------------------|  |  
|  |textfield                   |  |  
|  |----------------------------|  | 
|                                  |
|                  |Regenerate|    | 
|----------------------------------|  

MyCollectionView binds to a text array, clicking "Regenerate" button will fill new content to the text array, so that the content of each text fields in MyCollectionView gets udpated.

My question is, how to make the first text field get default focus?

I tried the following ways, but failed:

1. Invoking [NSWindow makeFirstResponder:] directly

- (IBAction) onRegenerate:(id)sender  
{  
   // Fill text array, update view  

   // Then make first responder
   // firstViewItem is an IBOutlet of MyCollectionViewItem
   // textField is an IBOutlet of NSTextField in collection view item
   [[self window] makeFirstResponder:[firstViewItem textField]];
}

Seems the problem is, when invoking [NSWindow makeFirstResponder:], even the content of MyCollectionView has been updated ([NSCollectionView setContent:] has been called), the UI hasn't been refreshed. So I can see that [NSWindow makeFirstResonders:] does set focus to the first text field, but quickly the collection view refreshes, and then focus losts.

2. Binding MyCollectionView to an array controller, binding selectionIndexes

Then I set the default selection index each time when clicking "Regenerate" button, but still no effect

3. Set nextKeyView in Interface Builder

    Window
       *
       |
  (initialFirstResponder)
       |
       |------> MyCollectionView
                          *
                          |
                      (nextKeyView)
                          |
                          |------>The View of MyCollectionViewItem
                                            *
                                            |
                                        (nextKeyView)
                                            |
                                            |----> NSTextField

4. Create a NSTimer, in timer event handler set focus to the first text field

I set the interval of timer to 0.5 second, it works...But this way is too tricky!

So, is it possible to know that the content view has been fully updated and then I can invoke makeFirstResponder? Or other easy to do that? Thanks in advance.

Oh. My environment: XCode 3.2.2, OSX 10.5.8