Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
Objective c 设置弹出按钮的操作_Objective C_Xcode_Cocoa_Nspopupbutton - Fatal编程技术网

Objective c 设置弹出按钮的操作

Objective c 设置弹出按钮的操作,objective-c,xcode,cocoa,nspopupbutton,Objective C,Xcode,Cocoa,Nspopupbutton,我以编程方式创建了一个NSPoupButton,并为我的选择创建了一个数组,如何为每个单独的数组选择创建一个setAction?谢谢 NSRect buttonRect = NSMakeRect(1705, 145, 78, 50); //Button Array. When I pick the choice it closes the diologue box NSArray *newArray; NSString *color1 = @"Blue Color"

我以编程方式创建了一个NSPoupButton,并为我的选择创建了一个数组,如何为每个单独的数组选择创建一个setAction?谢谢

NSRect buttonRect = NSMakeRect(1705, 145, 78, 50); 

    //Button Array.  When I pick the choice it closes the diologue box
    NSArray *newArray;
    NSString *color1 = @"Blue Color";
    NSString *color2 = @"Green Color";
    NSString *color3 = @"Clear Color";

    newArray = [NSArray arrayWithObjects: color1, color2, color3, nil];

    NSPopUpButton *button = [[NSPopUpButton alloc] initWithFrame:buttonRect pullsDown:YES];
    [self addSubview:button];
    [button addItemsWithTitles:newArray];
    //want my action for each individual string
    [button setAction:@selector(changeFilterColor)];

-(void) changeFilterColor
{
    NSLog(@"colorChanged");

}
您需要将协议添加到接口(.h文件):

创建
nspoupbutton
后。另外,使用
setAction:
删除该行

复制此委托方法:

-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
{
    if ([item.title isEqualToString:@"Blue Color"]) { ... } //etc
}

添加必要的if语句以完成比较。

另一个选项是使用最初在代码中使用的
setAction
方法。但对于选择调用的选择器,请将其设置为接收发送方对象。大概是这样的:

-(void)method:(NSMenuItem *)sender

然后,您可以验证发送者对象是否有效,并从中获取标题。我有点担心使用
NSMenuDelegate
进行此操作。

我应该在协议中添加什么?
-(无效)菜单:
?看起来您缺少一个[按钮setTarget:self]
-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
{
    if ([item.title isEqualToString:@"Blue Color"]) { ... } //etc
}
-(void)method:(NSMenuItem *)sender