Re: subclass NSAlert or what?
Re: subclass NSAlert or what?
- Subject: Re: subclass NSAlert or what?
- From: Günther Blaschek <email@hidden>
- Date: Tue, 12 Apr 2005 09:06:08 +0200
On 12.04.2005, at 07:21,
Johnny Deadman wrote:
the Apple dox for NSAlert say 'not designed for subclassing' but perhaps folks here can enlighten me as to how to solve this problem.
In the course of a user paste, I may need to throw up a dialog which tells a user that a style in what they just pasted isn't available in this document, and which of the available styles do they want to use to replace it.
The section of code looks something like this (edited for clarity):
if ( ! [[self documentStyle] doesContainElementStyleNamed: elementStyleName] )
{
GLElementStyle *replacementElementStyle =
[self userChosenReplacementElementStyleForElementStyleNamed: elementStyleName];
if ( replacementElementStyle == nil) return nil; // User aborted
// replace this elementStyle name through the string
returnString = [self replaceElementStyleName: elementStyleName
withElementStyleName: [replacementElementStyle name]
inAttributedString: returnString];
}
So I am wondering how to implement -userChosenReplacementElementStyleForElementStyleNamed:
It needs to throw up a dialog, which has a pop-up menu in it populated with alternatives, and return either the selected object or 'nil' if the user presses the 'cancel' button. Obviously I can create this in a NIB but I have no idea how to get the value the user chooses back to the controller. An NSAlert would be easy, but obviously this needs to be customized... do I have to make a windowController & all that jazz or is there a simpler way of doing it?
TIA,
JB
I'd recommend that you create a separate window (in a separate nib file) for this purpose with all the required user interface elements for picking an alternative (list or pop-up menu, OK and Cancel buttons, etc.).
When the user pastes something and you need to ask for the preferred style, open the window as a sheet:
[NSApp beginSheet:pickStyleSheet modalForWindow:myWindow modalDelegate:self
didEndSelector:@selector(pickStyleSheetEnded:returnCode:context:) contextInfo:nil];
The "modalDelegate:self" assumes that you call this from your controller and wish to reuse the controller for the sheet as well.
Attach the "OK" and "Cancel" buttons to new actions in your controller:
- (IBAction) cancelPickStyleSheet: (id) sender
{
[pickStyleSheet orderOut: sender];
[NSApp endSheet: pickStyleSheet returnCode:0];
}
- (IBAction) confirmPickStyleSheet: (id) sender
{
[pickStyleSheet orderOut: sender];
[NSApp endSheet: pickStyleSheet returnCode:1];
}
Finally, you need the pickStyleSheetEnded:returnCode:context: method to actually perform the paste:
- (void) pickStyleSheetEnded: (NSWindow*) sheet returnCode: (int) returnCode context: (void*) info
{
if (returnCode) { // will be YES for "OK"
// check the user's selection and do your paste stuff here
}
}
There's more about this, but I guess you'll be able to add the missing things yourself. Basically, you'll need outlets to access both the the "pick style" sheet window and the settings in the sheet window (to set up the list of options before you put up the sheet and to find out what the user actually selected).
However, you might also want to reconsider the user interface altogether. The pasteboard will contain multiple flavors. When the user selects "Paste", you could simply use the best variant available. To give the user more control about what to paste, you could add a separate "Paste As" menu with an attached submenu that lists the currently available formats.
From a user's perspective, I would prefer this technique, as it avoids modal states altogether.
HTH,
<x-tad-bigger>--
gue = Günther Blaschek
</x-tad-bigger>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden