简体   繁体   中英

Triggering an action by NSMenuItem

是否可以通过单击 NSMenuItem 触发与任何 ViewController 无关的操作?

Yes it is possible! Use target on property on NSMenuItem like this:

var newItem : NSMenuItem = NSMenuItem(title: "Quit", action: Selector("Quit:"), keyEquivalent: "")
newItem.target = // Your Target

Note that it has a target property. Docs: "An object to be the target of action messages sent by the receiver."

If I understand your question correctly, yes it is. You just have to use @selector with the associated function, so that when you click the NSMenuItem the function will be call. Here is an example:

[self.statusBarMenu addItem:[[NSMenuItem alloc] initWithTitle:@"My Menu Item" action:@selector(doSomething) keyEquivalent:@"D"]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM