I had created an NSMenu via Interface Builder. One of the menu items called showPreferencesPanel method, which is defined in KBAppController.m:
-(void)showPreferencesPanel {
//something
}
Now, I have to re-build the menu without IB…all programmatically. StatusMenu.m is the class that deals with all that, and I can’t figure out the target that I should set so that a method is called from another class.
Creating an instance of that class didn’t work! The menu item is grayed out.
StatusMenu.m
KBAppController *kbAppController = [[KBAppController alloc]init];
NSMenuItem* preferencesItem;
[preferencesItem setTarget:kbAppController];
preferencesItem = [[NSMenuItem alloc] initWithTitle:@"Preferences…" action:@selector(showPreferencesPanel) keyEquivalent:@""];
Edited:——————————————-
Here is the updated code that uses an object of class KBAppController. The good news is that the prefs menu item is enabled, but it still doesn’t call the method in KBAppController.m 🙁
KBStatusMenu.m
@synthesize kbAppController = _kbAppController;
someMethod {
NSMenuItem* preferencesItem;
preferencesItem = [[NSMenuItem alloc] initWithTitle:@"Preferences…" action:@selector(showPreferencesPanel) keyEquivalent:@""];
[preferencesItem setTarget:self];
}
- (void)showPreferencesPanel {
NSLog(@"in 1");
[_kbAppController showPreferencesPanel];
}
KBAppController.m
-(void)showPreferencesPanel {
NSLog(@"in 2");
//something
}
Have the
selectorcall the method from your class. And inside that method, callshowPreferencePanel()with the object ofKBAPPController.Make sure to set the object of KBAppController as a property of StatusMenu.