gpt4 book ai didi

objective-c - 以编程方式添加搜索文本框作为 Cocoa 中的菜单项

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

我想要一个像 iTunes 那样的搜索输入框。我有我的工具栏显示出来,我有下面的代码显示了一个自定义的toobaritem,它有正确的灰色描述,并以紫色显示文本“Fnurd..”,但文本不可编辑!这让我发疯,我做错了什么?

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];

if ( [itemIdentifier isEqualToString:@"SearchItem"] ) {

//Land in sight !
NSLog(@"### Setting up Search Item Menu Entry");

//Starter size
NSRect cellFrame = NSMakeRect(0,0,200,25);
//Create/allocate Control
searchMenuControl = [[NSControl alloc] initWithFrame:cellFrame];
//Create/allocate Cell
searchMenuCell = [[NSTextFieldCell alloc] initTextCell:@"Fnurd.."];

//Assign cell to control
[searchMenuControl setCell:searchMenuCell];

//Do some polishing
[searchMenuCell setBezelStyle:NSTextFieldRoundedBezel];
[searchMenuCell setBackgroundColor:[NSColor whiteColor]];
[searchMenuCell setTextColor:[NSColor blackColor]];
[searchMenuCell setEnabled:YES];
[searchMenuCell setCellAttribute:NSCellEditable to:YES];


cellFrame = [searchMenuControl frame];

// Configuration code for "SearchItem"
[item setLabel:@"Search Records"];
[item setPaletteLabel:[item label]];
[item setView:searchMenuControl];
[item setMinSize:cellFrame.size];
[item setMaxSize:cellFrame.size];
}

return [item autorelease];
}

最佳答案

发布到 stackoverflow 是一件很神奇的事情,您一定会在 google 查询后找到答案..

Cocoa 基本上有一个您可以使用的搜索字段小部件..

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];

if ( [itemIdentifier isEqualToString:@"SearchItem"] ) {

//Land in sight !
NSLog(@"### Setting up Search Item Menu Entry");

searchField = [[NSSearchField alloc] init];
[searchField sizeToFit];

NSRect cellFrame = [searchField frame];

// Configuration code for "SearchItem"
[item setLabel:@"Search"];
[item setPaletteLabel:[item label]];
[item setView:searchField];
[item setMinSize:cellFrame.size];
[item setMaxSize:cellFrame.size];
}

return [item autorelease];
}

关于objective-c - 以编程方式添加搜索文本框作为 Cocoa 中的菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824499/

25 4 0