0

I'm having some problems setting a custom NSView for my NSPopupButton menu items. Here is what I've got so far:

@interface ViewController ()

@property (weak) IBOutlet NSPopUpButton *popupButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    for(int i = 0; i < 25; i++) {
        NSMenuItem *menuItem = [[NSMenuItem alloc ] initWithTitle:[NSString stringWithFormat:@"%d", i] action:@selector(itemClicked:) keyEquivalent:@""];

        MenuView  *menuView = [[MenuView alloc] initWithFrame:CGRectMake(0, 0, 184, 50)];
        menuView.displayText.stringValue = @"This is a test";

        [menuItem setView:menuView];
        [self.popupButton.menu addItem:menuItem];
    }
}

- (void)itemClicked:(id)sender {

}

@end

//My custom view

@implementation MenuView
- (id)initWithFrame:(NSRect)frameRect {
    NSString* nibName = NSStringFromClass([self class]);
    self = [super initWithFrame:frameRect];
    if (self) {
        if ([[NSBundle mainBundle] loadNibNamed:nibName
                                          owner:self
                                topLevelObjects:nil]) {
            [self configureView];
        }
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configureView];
}

- (void)configureView {
    [self setWantsLayer:YES];
    self.layer.backgroundColor = [NSColor blueColor].CGColor;
}


@end

//Here is what my xib MenuView looks like

enter image description here

And here is the problem:

enter image description here

This seems like it should be a fairly straight forward task but I'm not sure what is happening to my view and why my label on the view seems to disappear and is not showing any text for each of the views. I was poking around in the documentation and stumbled across this for the NSPopupButton Menu :

// Overrides behavior of NSView.  This is the menu for the popup, not a context menu.  PopUpButtons do not have context menus.
@property (nullable, strong) NSMenu *menu;

I'm not sure if there is something that I'm doing wrong that is causing this problem or if what I'm trying to do in this context is not achievable off of an NSPopupButton NSMenu. If anyone has any experience with this and could offer advice I'd really appreciate it.

zic10
  • 2,310
  • 5
  • 30
  • 55
  • You use the view created with `initWithFrame`, this is not the view from the nib. – Willeke Apr 15 '16 at 12:31
  • Here's an explanation: [programmatically loading object from subclass of NSView from nib](http://stackoverflow.com/questions/5855154/programmatically-loading-object-from-subclass-of-nsview-from-nib) – Willeke Apr 15 '16 at 12:41

0 Answers0