views:

266

answers:

1

I'm trying to figure out how to pass a variable (in my case hostname) to a function i created from a programatically inserted "menu item".

#function

-(void)goToTerminal:(NSString *)hostname {
    NSString *s = [NSString stringWithFormat:@"tell application \"Terminal\" to do script \"ssh root@%@\"", hostname];
    NSAppleScript *as = [[NSAppleScript alloc] initWithSource: s];
    [as executeAndReturnError:nil];
}

# adding menu item in another function
[NSString *hostname = [NSString stringWithString:@"some.host"]];
NSMenuItem *subhostline = [[[NSMenuItem alloc] initWithTitle:sshtohost action:@selector(goToTerminal:) keyEquivalent:@""]autorelease];
[subhostline setTarget:self];

Thankyou! D.

+2  A: 

NSMenuItem has -tag and -setTag: methods that can be used to associate an arbitrary integer with a menu item. That integer could easily be the index of the hostname in an array.

Target/action methods take a single argument, the sender. The sender is the UI element that is firing the action method on the target. To preserve the separation inherent to MVC, you don't generally shove data directly into the UI elements; the UI-- the view layer-- performs an action against a controller which then picks the appropriate piece of data out of the model and does whatever it needs to do.

bbum
gotya! thanks! ;-)
SomeCoder