NSPopUpButton的SetAction正在禁用我的popUpButton

Akb*_*bar 7 macos cocoa nspopupbutton

我已使用以下代码以编程方式创建了NSPopUpButton

[myPopUpButton insertItemWithTitle:@"--Select one--" atIndex:0];
[myPopUpButton addItemsWithTitles:[NSArray arrayWithObjects:@"1.One",@"Two",@"Three", nil]];

[myPopUpButton sizeToFit];
[myPopUpButton  setAction:@selector(popUpAction:)];
[fullBrowserView addSubview: myPopUpButton];

//PopUp Action
-(void)popUpAction:(id)sender
{
    NSLog(@"popUpAction");
}
Run Code Online (Sandbox Code Playgroud)

当我单击popUpButton时,popUpButton的菜单项被禁用.当我使用interfacebuilder时,它正好适用于IBAction.

为什么这个setAction对NSPopUpButton不起作用?

pet*_*ert 13

看起来您没有设置目标对象来发送消息.所以,在代码中添加:

[myPopUpButton setTarget:self];
Run Code Online (Sandbox Code Playgroud)

假设该popUpAction:方法属于同一类.

当您使用Interface Builder时,它会将选择器操作连接到目标.

从此电话的文档:

- (void)setTarget:(id)anObject

如果anObject是,nil但控件仍然分配了有效的操作消息,则应用程序在响应者链后面查找可以响应该消息的对象.

在您的情况下,没有对象响应该消息.


lif*_*joy 6

即使myPopUpButton有目标和操作,您可能还需要添加:

[myPopUpButton setAutoenablesItems:NO];
Run Code Online (Sandbox Code Playgroud)

否则,每次单击该按钮,它都会自动禁用其菜单上的所有项目.(我意识到这个问题已经过时了,但发布这个解决方案以防万一)