question about first responder

Discussion in 'Mac Programming' started by Howiieque, Jan 16, 2010.

  1. Howiieque macrumors regular

    Joined:
    Feb 1, 2009
    #1
    hello, everybody.
    i have a nswindow and it contains myview. the myview has a myviewcontroller.

    1.
    within the myviewcontroller.m,
    ...
    [[myview window] makeFirstResponder: myview];
    …

    then whether the myview' s acceptsFirstResponder: method returns YES or NO, myview will become the first responder.

    according to apple's documentation, it should not be the first responder, when acceptsFirstResponder: method returns NO.

    2.
    if i set override the acceptsFirstResponder: and make it return YES. the myview becomes the first responder automatically. i even don't need to send makeFirstResponder: or set it in the IB.

    why would it have have the chance to became the first responder?

    could someone explain these? it's very confusion.
     
  2. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    Because the first responder may need to change its state from inactive to active. A Text Box for example will have the cursor blinking, a focus ring around it and any text typed will end up inside it.
     
  3. Howiieque thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #3
    but the view explicitly says it refuse to become the first responder. but why does it end up being the first responder?
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    Two things happen to make a view the first responder. The method above lets the view's controller know that the view can become first responder. But then the view must return "YES" to the method -becomeFirstResponder. This method is defined in NSResponder, which is a superclass of NSView and all its various subclasses, and by default, it returns "YES", where as NSResponder's default value for -acceptsFirstResponder is "NO". If you return "YES" to -acceptsFirstResponder, your view will automatically become first responder and keep that status as long as no other view takes it.

    First responder status is really only important in two ways. It allows a view to subclass the methods that handle the keyboard, and it relates to nil-targeted actions (such as an menu action that sends to first responder). If neither of these things are used in your myview class, first responder status does not really matter.
     
  5. Howiieque thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #5
    thank you very much.

    but even the view returns NO, the controller can still make it the first responder. could you explain a bit about the reason.

    and if there is more than one view(or its subview, button, switch, etc) returns NO, which will become the first responder. say a view contain text fields and button.
     

Share This Page