gpt4 book ai didi

objective-c - 设置 PopUpButton 的操作

转载 作者:行者123 更新时间:2023-12-03 16:25:15 25 4
gpt4 key购买 nike

我以编程方式创建了一个 NSPopUpButton,并为我的选择创建了一个数组,如何为每个单独的数组选择创建一个 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");

}

最佳答案

您需要添加 NSMenuDelegate接口(interface)协议(protocol)(.h 文件):

@interface MyClass : NSObject <NSMenuDelegate>

然后:

[[button menu]setDelegate:self];

创建NSPopUpButton之后。另外,删除带有 setAction: 的行。

复制此委托(delegate)方法:

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

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

关于objective-c - 设置 PopUpButton 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com