365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

Apr 3

Day 35: NSResponder actions (4)

Today we’ll be actually creating our actions and inserting them in the responder chain. We keep a dictionary of all our actions in case we need to perform actions programatically and, as we’ll see tomorrow, for our toolbar.

The setup is done in a method named -createActions which finds a responder the views have in common and then does its thing. -actionClassNames returns an array with all our actions (possibly read from a plist file) and -actionForDocument: is the default initializer for my actions.

- (void)createActions
{
  id lastResponder = ....
  id nextResponder = [lastResponder nextResponder];
    
  actions = [[NSMutableDictionary alloc] init];
  
  for (NSString *name in [self actionClassNames]) {
    MSBaseAction *action = [NSClassFromString(name) actionForDocument:self];
    [actions setValue:action forKey:name];
    [lastResponder setNextResponder:action];
    lastResponder = action;
  }
  [lastResponder setNextResponder:nextResponder];
}

  1. 365cocoa posted this