Re: How to position a window under a button of another window?
Re: How to position a window under a button of another window?
- Subject: Re: How to position a window under a button of another window?
- From: Erik Buck <email@hidden>
- Date: Tue, 17 Apr 2007 06:08:47 -0700 (PDT)
I have built a simple working example of a pop-up child window containing a slider complete with an xCode project: http://www.stepwise.com/EMB/PopupHelper/PopupHelper.zip
Here is how it works:
1) In Interface Builder, place a small button next to whatever NSControl you want to give a pop-up helper. For example, if you have an NSTextField in IB, place a small button next to it.
2) Create an instance of PopupHelper? class (source code below) in the same nib as the NSControl you want to give a pop-up helper.
3) Connect the PopupHelper? instance's controlToHelp outlet to the control that needs help.
4) Connect the target/action of the button to the PopupHelper? instance's -toggleShowHelper: action.
5) Create a new empty nib. Drag an Custom View into the instances area of the empty nib. Then place whatever controls you want in the custom view. Sliders and button work well in the custom view, but text views do not because they want to be in a key window and popup helper windows never become key.
6) Make the File's Owner of the new nib be of class PopupHelper?.
7) Bind the values of the controls in the custom view to whatever you want using the File's Owner->controlToHelp key path. For example, bind a sliders value to File's Owner->controlToHelp.floatValue
8) Save the new nib in your project.
9) Set the title of the button next to the control to help to the name of the nib you just saved. This is a hack, but it works well if the button is set to display icon only. The title will never be seen by a user. The title of the button is just a convenient way to identify which nib to load when it is necessary to display the popup helper.
10) Build and run. When the button next to the control to help is pressed, the nib containing helper controls is loaded and the controls are added to a child window positioned next to the control that needs help. There are several
subtleties:
A) The control to help is made into the firstResponder when the popup is displayed.
B) The value binding only works one way unless you properly use Key Value Observing, but it is seldom an issue and is mitigated if you bind the helper controls to model values instead of the control to help.
C) Moving the window containing the control to help works fine, but resizing the window can cause temporary misalignment of the helper popup and the control to help. The easiest way to solve the problem is to just hide all popups when the window is resized. See + (void)hideAllPopupHelpers;
D) Because of the the dynamic nib loading approach, you can have complex popup helpers and different popup helpers for every control if you want...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden