objective-cnssearchfield

highlight NSSearchField


I have a NSWindow which consist a NSView which appear only on some specific occasion this NSView consist a NSSearchField I want if there is data on clipboard it will appear in searchField otherwise it would be empty.I am able to do this what I want is if there is Data in SearchField it should be in focus I tried it this way:

In NSViewController class there is a function which returns NSSearchField which is a class variable

              -(NSSearchField*)getSearchField
                  {
                  return searchField;
                  } 

In NSWindowController class I am making it first responder where pSearchContact is instance variable of NSViewController class

      [[self window] makefirstResponder:[pSearchContact getSearchField]];

It is running smoothly but I don't know why searchField is not getting focus

Is their something like searchField will become first responder only if it is a part of NSWindow because in my case searchField is in NSView which is in NSWindow.

Thanks in Advance


Solution

  • Your question is a little confusing but I think what you're asking to boils down to "why can't I ever make my search field the focus?".

    That one line of code:

    [[self window] makefirstResponder:[pSearchContact getSearchField]];
    

    has a little too much going on with it for my comfort (i.e. I wouldn't embed so much functionality - any pieces of which could go wrong or haywire - into one line of code).

    How about doing something like this:

    NSWindow * myWindow = [self window];
    if(myWindow)
    {
        if(pSearchContact)
        {
            NSResponder * ourSearchField = [pSearchContact getSearchField];
            if(ourSearchField)
            {
                [myWindow makeFirstResponder: ourSearchField];
            } else {
                NSLog( @"ourSearchfield is nil; why?" );
            }
        } else {
            NSLog( @"pSearchContact is nil; why?" );
        }
    } else {
        NSLog( @"myWindow is nil; why?" );
    }
    

    This might also allow you to narrow down on why the focus setting isn't working for you.