Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa NSPOUPPBUTON未选择右侧_Cocoa_Selection_Objective C 2.0_Nspopupbutton - Fatal编程技术网

Cocoa NSPOUPPBUTON未选择右侧

Cocoa NSPOUPPBUTON未选择右侧,cocoa,selection,objective-c-2.0,nspopupbutton,Cocoa,Selection,Objective C 2.0,Nspopupbutton,我正在使用此代码将弹出按钮添加到NSView: if (popupButton) [popupButton release]; popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, SHEET_WIDTH/2, 32) pullsDown:true]; NSMenu *menu = [[NSMenu alloc] init]; for (NSString *title in anArray) [menu a

我正在使用此代码将弹出按钮添加到
NSView

if (popupButton) [popupButton release];
popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, SHEET_WIDTH/2, 32) pullsDown:true];
NSMenu *menu = [[NSMenu alloc] init];
for (NSString *title in anArray)
    [menu addItemWithTitle:title action:NULL keyEquivalent:@""];
[popupButton setMenu:menu];
[self addView:popupButton aligned:KOAlignmentCenter];
当我启动应用程序时,按钮没有选择。当用户单击该按钮并选择其中一项时,该按钮将保持为空。例如,如果有3个可能的选择(item1、item2和item3),并且用户单击第二个选项,而不是显示“item2”,则不会显示任何内容:


我不知道为什么你没有看到任何东西,因为当我尝试你的代码时,我得到了数组中显示的第一项。但是,从列表中拾取项目不会更改显示的内容,这是下拉式按钮的预期行为。来自苹果的文档:

下拉列表通常在弹出窗口旁边显示 按钮,与子菜单显示在其父项旁边的方式相同。 与弹出列表不同,显示下拉列表的弹出按钮的标题 列表不基于当前选定的项目,因此保留 除非使用单元格的setTitle:方法进行更改,否则已修复

您也不需要这两个菜单语句,只需在循环中使用NSPopupButton方法addItemWithTitle:。因此,在没有菜单命令的情况下尝试,如果最初仍然没有显示任何内容,请显式地使用setTitle:。或者,您可以更改为弹出窗口而不是下拉,这样您就不会有设置标题的问题

这就是我所做的测试:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSArray *anArray = @[@"One",@"Two",@"Three",@"Four"];
    _popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(100, 200, 200, 32) pullsDown:TRUE];
    for (NSString *title in anArray)
        [_popupButton addItemWithTitle:title];
    [self.window.contentView addSubview:_popupButton];
}

我不知道为什么你没有看到任何东西,因为当我尝试你的代码时,我得到了数组中第一个显示的项目。但是,从列表中拾取项目不会更改显示的内容,这是下拉式按钮的预期行为。来自苹果的文档:

下拉列表通常在弹出窗口旁边显示 按钮,与子菜单显示在其父项旁边的方式相同。 与弹出列表不同,显示下拉列表的弹出按钮的标题 列表不基于当前选定的项目,因此保留 除非使用单元格的setTitle:方法进行更改,否则已修复

您也不需要这两个菜单语句,只需在循环中使用NSPopupButton方法addItemWithTitle:。因此,在没有菜单命令的情况下尝试,如果最初仍然没有显示任何内容,请显式地使用setTitle:。或者,您可以更改为弹出窗口而不是下拉,这样您就不会有设置标题的问题

这就是我所做的测试:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSArray *anArray = @[@"One",@"Two",@"Three",@"Four"];
    _popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(100, 200, 200, 32) pullsDown:TRUE];
    for (NSString *title in anArray)
        [_popupButton addItemWithTitle:title];
    [self.window.contentView addSubview:_popupButton];
}

我会试试这个,然后再报告(如果可行的话,我会选择最佳答案)。已经谢谢你的麻烦了!!!当我使用弹出按钮并使用您的代码时,它会工作。谢谢你,伙计!谢谢你,这让我想尽办法弄明白为什么标题没有改变!真正让人恼火的是,我已经查看了好几次NSPopupButton的文档,但都没有看到您提到的引用。我会尝试一下,然后再报告(如果有效的话,我会选择最佳答案)。已经谢谢你的麻烦了!!!当我使用弹出按钮并使用您的代码时,它会工作。谢谢你,伙计!谢谢你,这让我想尽办法弄明白为什么标题没有改变!真正令人恼火的是,我已经查阅了好几次NSpoupButton的文档,但我没有看到您提到的任何地方的引用。