NSPopupButton NSMenu

时间:2016-04-15 01:54:50

标签: objective-c macos appkit nsmenu nspopupbutton

我在为NSPopupButton菜单项设置自定义NSView时遇到一些问题。这是我到目前为止所得到的:

@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

//我的自定义视图

@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

//这是我的xib MenuView的样子

enter image description here

问题在于:

enter image description here

这似乎应该是一个相当直接的任务,但我不确定我的观点发生了什么以及为什么我在视图上的标签似乎消失了,并且没有为每个视图显示任何文本。我在文档中逛了一圈,偶然发现了NSPopupButton菜单:

// 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;

我不确定是否有什么东西我做错导致了这个问题,或者我在NSPopupButton NSMenu中无法实现我在这种情况下要做的事情。如果有人对此有任何经验并且可以提供建议我会非常感激。

0 个答案:

没有答案