How to create NSButton with delayed NSMenu? - cocoa

In Cocoa i want to create an nsbutton with delayed menu.
i.e., When clicked it should call the action method and when kept in pressed state for 2 seconds it should display a nsmenu.
It is similar to "Build Active Target" button present in Xcode toolbar.
Regards,
Dhanaraj.

It's a NSPopUpButton.. Here is how I create it in my app.
NSToolbarItem* item = [[[NSToolbarItem alloc] initWithItemIdentifier:identifier] autorelease];
[item setLabel:label];
[item setPaletteLabel:label];
[item setToolTip:tooltip];
NSPopUpButton* button = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 24) pullsDown:NO] autorelease];
NSMenu* menu = [button menu];
// insert code here, that adds NSMenuItems to the menu
[button setTarget:self];
[button setAction:#selector(menuAction:)];
[[button cell] setBezelStyle:NSTexturedRoundedBezelStyle];
[[button cell] setArrowPosition:NSPopUpArrowAtBottom];
[[button cell] setFont:[NSFont systemFontOfSize:14]];
[item setView:button];

Related

How to get the selected NSMenuItem from a submenu?

I have an API that allows users to create popup menus with sub menus, and I'm having problems detecting the selected item when the user clicks on an option that belongs to a sub menu.
So, the display and "construction" of the popup menu is correct and it works fine. Starting from a NSMenu I add a few NSMenuItems, then for some NSMenuItems I add a new NSMenu with a few NSMenuItems more.
The problem begins when I click on an item that belongs to a submenu, the selectedItem I get is always nil. It works fine for the main menu. Here's how I show the menu:
NSRect frame = NSMakeRect(mp.origin.x + 10, mp.origin.y + 10, 1, 1);
NSPopUpButtonCell *cell = [[NSPopUpButtonCell alloc] initTextCell: #"" pullsDown: NO];
[cell setAutoenablesItems: NO];
[cell setAltersStateOfSelectedItem: NO];
[cell setMenu: mainMenu];
[cell selectItem: Nil];
[cell performClickWithFrame: frame inView: [window initialFirstResponder]];
NSMenuItem *xpto = [cell selectedItem];

putting buttons on masterdetail template xcode

My question is, what is the most appropriate way to put my own buttons on the upper bar of the Master view in a MasterDetail template.
I would like to put my own buttons on the Top portion of the Master view in the MasterDetail template. It arrives with an edit button on the left and an add (+) button on the right. I commented out the Edit button in the .m file. I drug in my own button in its place. The first thing I noticed is that my button looks different then the + button on the right. It seems to be of uniform size. The next thing is that when I created my button, it did not have a place to specify the Event (touch up inside). Could I have somehow modified the self.editButtonItem. Surely this is a common thing to do - to replace their buttons w/ ours. thaks
Create two custom buttons and set on the navigation item..
// Set navigation left button
UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLeft setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnLeft addTarget:self action:#selector(skipImage:) forControlEvents:UIControlEventTouchUpInside];
[btnLeft setFrame:CGRectMake(0, 0, 60, 29)];
[btnLeft setTitle:#"Skip" forState:UIControlStateNormal];
[btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLeft .titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
//
// Set navigation right button
btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRight setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnRight addTarget:self action:#selector(nextImage:) forControlEvents:UIControlEventTouchUpInside];
[btnRight setFrame:CGRectMake(0, 0, 60, 29)];
[btnRight setTitle:#"Next" forState:UIControlStateNormal];
[btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnRight.titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
[self.navigationItem setRightBarButtonItem:rightBarButton];
//
This example should solve your problem: http://rogchap.com/2011/06/21/custom-navigation-bar-background-and-custom-buttons/ .. originally answered here - > Custom Navigation Bar Buttons..

UIButton in PopoverView with own XIB should send action to mainview

I have a popover view which is called with:
Info *infoview = [[Info alloc]init];
pop = [[UIPopoverController alloc]initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400) inView:bigMenuView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
In this popoverview is (see in the code) an info.xib which has some UIBUttons.
Now I want to change something. in my "mainview" after I click on the Button.
Note: I can access my IBAction Method in the "First Responder" but the is no action.
In order to do what you want, declare a property on the button in Info, something like :
#property(retain) IBOutlet UIButton *myButton;
Now synthesize it, in implementation part. In order for the button to send the correct action to the correct target, you should do like :
Info *infoview = [[Info alloc]init];
// set the action/target here
[infoView.myButton addTarget:bigMenuView
action:#selector(theSELYouWant)
forControlEvents:UIControlEventTouchUpInside];
pop = [[UIPopoverController alloc] initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400)
inView:bigMenuView
permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
I supposed that the main view is bigMenuView.
This will make the button sending a theSELYouWant message to bigMenuView each time it is touched.

how to make NSTextField editable

I am creating a custom class which is inherited from NSPanel and this panel is my landing screen of the application.
I am adding a NSTextField on it, which is visible on the screen. Problem is that the textfield is not editable.
However, if i create a new cocoa project and run the same addSubview code for textfield, all is good, i am able to edit the textfield.
It seems to me like problem is with my custom panel, but i am not able to hunt it down.
Here is my code:
NSTextField *infoTextField = [[NSTextField alloc] initWithFrame:rect];
[[window contentView] addSubview:infoTextField];
[infoTextField setDelegate:self];
[[infoTextField window] becomeFirstResponder];
[infoTextField setTextColor:[NSColor blackColor]];
[infoTextField setDrawsBackground:YES];
[infoTextField setBordered:YES];
[infoTextField setSelectable:YES];
[infoTextField setEditable:YES];
[infoTextField setEnabled:YES];
[infoTextField setAlignment:NSLeftTextAlignment];
[infoTextField setStringValue:#"What are you doing?"];
[infoTextField release];
I need your help...
Override the method in NSPanel subclass,
- (BOOL)canBecomeKeyWindow {
return YES;
}

Hide NSMenu programmatically from NSStatusItem

I have this application that shows an item in the system's status bar, and one of the items is a custom view with a NSTextField and a NSButton. When the user clicks on the status bar item, it shows the menu, the user inputs some text and presses the button. This triggers an action that displays a window.
The problem I'm having now is, when the button is pressed it does trigger the action, but the menu remains visible. I want to hide the menu, because the action has already been processed.
I've searched through the API, but couldn't find how to do it.
Any ideas?
This is how I'm creating the menu:
NSStatusBar *bar = [NSStatusBar systemStatusBar];
self.statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[statusItem setImage:[NSImage imageNamed:#"icon_status_bar.png"]];
[statusItem setHighlightMode:YES];
NSMenuItem *textInputItem = [[NSMenuItem alloc] initWithTitle:#"" action:nil keyEquivalent:#""];
[textInputItem setView:myCustomView]; // created on the Nib file...
NSMenu *menu = [[NSMenu alloc] initWithTitle:NSLocalizedString(#"statusBarMenuTitle", #"")];
[menu addItem:textInputItem];
[statusItem setMenu:menu];
[textInputItem release];
[menu release];
It's not obvious in the docs, but [menu cancelTracking] is what you want.
cancelTracking
Dismisses the menu and ends all menu tracking.
- (void)cancelTracking

Resources