Add programmatically search text box as a menu item in Cocoa
I want to have a search input box like iTunes has. I have my toolbar which shows up, I have the below code which shows a custom toobaritem, it has the right description in grey and shows the text 'Fnurd..' in purple, but the text is not editable! This is driving me insane, what am I doing wrong ?
- (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");
//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];
[se开发者_开发知识库archMenuCell 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];
}
Posting to stackoverflow is magic, you are bound to find the answer one google query later..
Cocoa basically has a search field widget that you can use..
- (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];
}
Try setting your searchMenuCell to editable.
精彩评论