0

I need to use a custom view into a NSMenuItem. I've created a new view XIB and customized the view in it. How can I load that view and set it in the NSMenuItem using the setView: method?

UPDATE: I've found a solution but now the menu item with the custom view doesn't highlight on mouse over. Ho can I solve this problem?

Francesco Papagno
  • 617
  • 10
  • 29

1 Answers1

2

I've created a NSViewController subclass and set it as the file's owner in the XIB. Then I've set the view outlet of the file's owner to the view in the XIB and finally when needed I've instantiated the view controller and set it's view as the NSMenuItem view with the following code:

CustomViewController *viewController = [[CustomViewController alloc] initWithNibName:@"NibName" bundle:nil];

NSMenuItem *menuItem = [[NSMenuItem alloc] init];
[menuItem setView:[viewController view]];
Francesco Papagno
  • 617
  • 10
  • 29
  • The menu item with the custom view doesn't highlight on mouse over. Ho can I solve this problem? – Francesco Papagno Sep 19 '10 at 11:36
  • 1
    I needed to add the line `[viewContoller loadView];` right after creating the controller. As far as highlighting the view, my guess is that you'll need to do all that manually. Track the mouse when it's in the view and redraw as needed. – Gary Makin Apr 02 '12 at 13:03