NSReponder chain and controllers

  • My code is getting ugly so I suspect I'm doing something wrong.  I'm
    trying to hook up menu items in my main menu to actions that I've
    defined in my controllers.  I have a Core Data doc with a multiple
    master-detail view hierarchy, and for testing purposes have buttons
    connected to  a wide variety of methods.  The NSArrayController
    subclasses are not in the responder chain so I'm connecting the menu
    items to the first responder, catching the action messages in my
    document and then dispatch them to the correct controller.  That
    translates into a lot of simple dispatch code.  I could move all the
    intelligence to the document level but that strikes me as  equally
    ugly.  Any suggestions?
    Thanks,
    Rob
  • Add your controllers to the responder chain when appropriate and then
    put the IBAction methods in the relevant controllers.  If you
    duplicate methods (like delete: or cancel:) take care as to what order
    you add them into the chain because the first to respond wins (though
    it can always send it to its next responder if it doesn't make sense
    at the moment, like cancel: without an operation running).

    Adam Knight
    "Every man is guilty of all the good he didn't do."  -- Voltaire

    On Jun 19, 2008, at 1:20 PM, Robert Douglas wrote:

    > My code is getting ugly so I suspect I'm doing something wrong.  I'm
    > trying to hook up menu items in my main menu to actions that I've
    > defined in my controllers.  I have a Core Data doc with a multiple
    > master-detail view hierarchy, and for testing purposes have buttons
    > connected to  a wide variety of methods.  The NSArrayController
    > subclasses are not in the responder chain so I'm connecting the menu
    > items to the first responder, catching the action messages in my
    > document and then dispatch them to the correct controller.  That
    > translates into a lot of simple dispatch code.  I could move all the
    > intelligence to the document level but that strikes me as  equally
    > ugly.  Any suggestions?
  • NSArrayController isn't an NSResponder, so you can't add your
    subclasses to the responder chain.  I think the problem is that you
    should be using NSWindowController and NSViewControllers to handle
    menu actions, not NSArrayController.  Those objects can be added to
    the responder chain so you don't have to worry about dispatching
    anything in your code.

    On Jun 19, 2008, at 3:05 PM, Adam Knight wrote:

    > Add your controllers to the responder chain when appropriate and
    > then put the IBAction methods in the relevant controllers.  If you
    > duplicate methods (like delete: or cancel:) take care as to what
    > order you add them into the chain because the first to respond wins
    > (though it can always send it to its next responder if it doesn't
    > make sense at the moment, like cancel: without an operation running).
    >
    > Adam Knight
    > "Every man is guilty of all the good he didn't do."  -- Voltaire
    >
    >
    >
    > On Jun 19, 2008, at 1:20 PM, Robert Douglas wrote:
    >
    >> My code is getting ugly so I suspect I'm doing something wrong.
    >> I'm trying to hook up menu items in my main menu to actions that
    >> I've defined in my controllers.  I have a Core Data doc with a
    >> multiple master-detail view hierarchy, and for testing purposes
    >> have buttons connected to  a wide variety of methods.  The
    >> NSArrayController subclasses are not in the responder chain so I'm
    >> connecting the menu items to the first responder, catching the
    >> action messages in my document and then dispatch them to the
    >> correct controller.  That translates into a lot of simple dispatch
    >> code.  I could move all the intelligence to the document level but
    >> that strikes me as  equally ugly.  Any suggestions?

  • Thanks for the reminder about NSViewController.  I make heavy use of
    the NSArrayController methods but I suppose  I can move most of my
    code over simply by using an outlet and changing self to
    myArrayController.  But having two controllers simply to handle menu
    actions  still doesn't seems elegant, but it is better than what I'm
    doing now.
    -Rob

    On 19-Jun-08, at 12:33 PM, Cathy Shive wrote:

    > NSArrayController isn't an NSResponder, so you can't add your
    > subclasses to the responder chain.  I think the problem is that you
    > should be using NSWindowController and NSViewControllers to handle
    > menu actions, not NSArrayController.  Those objects can be added to
    > the responder chain so you don't have to worry about dispatching
    > anything in your code.
    >
    > On Jun 19, 2008, at 3:05 PM, Adam Knight wrote:
    >
    >> Add your controllers to the responder chain when appropriate and
    >> then put the IBAction methods in the relevant controllers.  If you
    >> duplicate methods (like delete: or cancel:) take care as to what
    >> order you add them into the chain because the first to respond wins
    >> (though it can always send it to its next responder if it doesn't
    >> make sense at the moment, like cancel: without an operation running).
    >>
    >> Adam Knight
    >> "Every man is guilty of all the good he didn't do."  -- Voltaire
    >>
    >>
    >>
    >> On Jun 19, 2008, at 1:20 PM, Robert Douglas wrote:
    >>
    >>> My code is getting ugly so I suspect I'm doing something wrong.
    >>> I'm trying to hook up menu items in my main menu to actions that
    >>> I've defined in my controllers.  I have a Core Data doc with a
    >>> multiple master-detail view hierarchy, and for testing purposes
    >>> have buttons connected to  a wide variety of methods.  The
    >>> NSArrayController subclasses are not in the responder chain so I'm
    >>> connecting the menu items to the first responder, catching the
    >>> action messages in my document and then dispatch them to the
    >>> correct controller.  That translates into a lot of simple dispatch
    >>> code.  I could move all the intelligence to the document level but
    >>> that strikes me as  equally ugly.  Any suggestions?

    >
  • Sometimes dispatching from First Responder to some other object is
    necessary.

    But there is an easy way to do it other than capturing each individual
    message and forwarding it - just use "invocation forwarding" which
    will do that automatically.

    http://www.cocoadev.com/index.pl?NSInvocation

    http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articl
    es/chapter_13_section_5.html#/

    /apple_ref/doc/uid/TP30001163-CH9-SW5

    hth,

    Graham

    On 20 Jun 2008, at 8:19 am, Robert Douglas wrote:

    > Thanks for the reminder about NSViewController.  I make heavy use of
    > the NSArrayController methods but I suppose  I can move most of my
    > code over simply by using an outlet and changing self to
    > myArrayController.  But having two controllers simply to handle
    > menu actions  still doesn't seems elegant, but it is better than
    > what I'm doing now.
    > -Rob
    >
    > On 19-Jun-08, at 12:33 PM, Cathy Shive wrote:
    >
    >> NSArrayController isn't an NSResponder, so you can't add your
    >> subclasses to the responder chain.  I think the problem is that you
    >> should be using NSWindowController and NSViewControllers to handle
    >> menu actions, not NSArrayController.  Those objects can be added to
    >> the responder chain so you don't have to worry about dispatching
    >> anything in your code.
    >>
    >> On Jun 19, 2008, at 3:05 PM, Adam Knight wrote:
    >>
    >>> Add your controllers to the responder chain when appropriate and
    >>> then put the IBAction methods in the relevant controllers.  If you
    >>> duplicate methods (like delete: or cancel:) take care as to what
    >>> order you add them into the chain because the first to respond
    >>> wins (though it can always send it to its next responder if it
    >>> doesn't make sense at the moment, like cancel: without an
    >>> operation running).
    >>>
    >>> Adam Knight
    >>> "Every man is guilty of all the good he didn't do."  -- Voltaire
    >>>
    >>>
    >>>
    >>> On Jun 19, 2008, at 1:20 PM, Robert Douglas wrote:
    >>>
    >>>> My code is getting ugly so I suspect I'm doing something wrong.
    >>>> I'm trying to hook up menu items in my main menu to actions that
    >>>> I've defined in my controllers.  I have a Core Data doc with a
    >>>> multiple master-detail view hierarchy, and for testing purposes
    >>>> have buttons connected to  a wide variety of methods.  The
    >>>> NSArrayController subclasses are not in the responder chain so
    >>>> I'm connecting the menu items to the first responder, catching
    >>>> the action messages in my document and then dispatch them to the
    >>>> correct controller.  That translates into a lot of simple
    >>>> dispatch code.  I could move all the intelligence to the document
    >>>> level but that strikes me as  equally ugly.  Any suggestions?
    >>>
  • Cool.  I'll probably end up doing something more like Cathy
    recommended but forwardInvocation looks  very powerful and useful.
    Rob

    On 19-Jun-08, at 7:02 PM, Graham Cox wrote:

    > Sometimes dispatching from First Responder to some other object is
    > necessary.
    >
    > But there is an easy way to do it other than capturing each
    > individual message and forwarding it - just use "invocation
    > forwarding" which will do that automatically.
    >
    > http://www.cocoadev.com/index.pl?NSInvocation
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articl
    es/chapter_13_section_5.html#/

    > /apple_ref/doc/uid/TP30001163-CH9-SW5
    >
    >
    >
    > hth,
    >
    > Graham
    >
    >
    >
    > On 20 Jun 2008, at 8:19 am, Robert Douglas wrote:
    >
    >> Thanks for the reminder about NSViewController.  I make heavy use
    >> of the NSArrayController methods but I suppose  I can move most of
    >> my code over simply by using an outlet and changing self to
    >> myArrayController.  But having two controllers simply to handle
    >> menu actions  still doesn't seems elegant, but it is better than
    >> what I'm doing now.
    >> -Rob
    >>
    >> On 19-Jun-08, at 12:33 PM, Cathy Shive wrote:
    >>
    >>> NSArrayController isn't an NSResponder, so you can't add your
    >>> subclasses to the responder chain.  I think the problem is that
    >>> you should be using NSWindowController and NSViewControllers to
    >>> handle menu actions, not NSArrayController.  Those objects can be
    >>> added to the responder chain so you don't have to worry about
    >>> dispatching anything in your code.
    >>>
    >>> On Jun 19, 2008, at 3:05 PM, Adam Knight wrote:
    >>>
    >>>> Add your controllers to the responder chain when appropriate and
    >>>> then put the IBAction methods in the relevant controllers.  If
    >>>> you duplicate methods (like delete: or cancel:) take care as to
    >>>> what order you add them into the chain because the first to
    >>>> respond wins (though it can always send it to its next responder
    >>>> if it doesn't make sense at the moment, like cancel: without an
    >>>> operation running).
    >>>>
    >>>> Adam Knight
    >>>> "Every man is guilty of all the good he didn't do."  -- Voltaire
    >>>>
    >>>>
    >>>>
    >>>> On Jun 19, 2008, at 1:20 PM, Robert Douglas wrote:
    >>>>
    >>>>> My code is getting ugly so I suspect I'm doing something wrong.
    >>>>> I'm trying to hook up menu items in my main menu to actions that
    >>>>> I've defined in my controllers.  I have a Core Data doc with a
    >>>>> multiple master-detail view hierarchy, and for testing purposes
    >>>>> have buttons connected to  a wide variety of methods.  The
    >>>>> NSArrayController subclasses are not in the responder chain so
    >>>>> I'm connecting the menu items to the first responder, catching
    >>>>> the action messages in my document and then dispatch them to the
    >>>>> correct controller.  That translates into a lot of simple
    >>>>> dispatch code.  I could move all the intelligence to the
    >>>>> document level but that strikes me as  equally ugly.  Any
    >>>>> suggestions?
    >>>>
  • On Fri, Jun 20, 2008 at 11:01 AM, Robert Douglas <rdouglas...> wrote:
    > Cool.  I'll probably end up doing something more like Cathy recommended but
    > forwardInvocation looks  very powerful and useful.

    I'd say that's almost an understatement.  It's the core of the Cocoa
    implementation of the delegate pattern, in fact.  There are certain
    cases where it will be invaluable -- and there will be many more cases
    where it's the more complicated alternative to a problem that has a
    simpler solution.  :)  The key is to understand when you need the
    power of the message-oriented aspect of Objective-C.

    --Kyle Sluder
previous month june 2008 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today