Re: [Q] SavePanel NSFileHandlingPanelCancelButton
Re: [Q] SavePanel NSFileHandlingPanelCancelButton
- Subject: Re: [Q] SavePanel NSFileHandlingPanelCancelButton
- From: Andy Lee <email@hidden>
- Date: Sun, 16 Jun 2002 12:14:21 -0400
At 8:06 AM -0700 6/16/02, Matt Neuburg wrote:
>From: Mark de Jong <email@hidden>
I'm trying to change the title of the cancel button in the NSSavePanel.
According to the documentation, I should be able to use the constant
"NSFileHandlingPanelCancelButton" to get at the NSButton.
From what I understand, the following code should work:
cancelButton = [[panel contentView]
>viewWithTag:NSFileHandlingPanelCancelButton];
[cancelButton setTitle:@"Quit"];
Interesting assumption. I don't see anything in the docs that associates
the NS... constants for NSSavePanel with the *tags* for objects in the
panel.
The association is sort of suggested in
<file:///System/Library/Frameworks/AppKit.framework/Versions/C/Resources/English.lproj/Documentation/Reference/ObjC_classic/TypesAndConstants/AppKitTypes.html>:
<snip>
NSSavePanel-Tags for Subviews
enum {
NSFileHandlingPanelImageButton = 150,
NSFileHandlingPanelTitleField = 151,
NSFileHandlingPanelBrowser = 152,
NSFileHandlingPanelCancelButton = NSCancelButton,
NSFileHandlingPanelOKButton = NSOKButton,
NSFileHandlingPanelForm = 155
};
Discussion
These constants are described in NSSavePanel .
</snip>
so maybe that's where Mark got the idea. The NSSavePanel doc only
says these constants are *returned* by -runModal... methods, but it
may in fact be true that each button's tag is actually the
corresponding enum value listed above. In fact, that would make
perfect sense.
The problem with using -viewWithTag: is that
NSFileHandlingPanelCancelButton is zero. The panel's content view
probably has plenty of descendant views with a zero tag, so it's not
surprising the Cancel button wasn't the view returned by
-viewWithTag:.
>So, the question is: how do I correctly change the title of the cancel
button?
Maybe this is isn't "correctly" but it works for me:
- (id) seekCancelButton: (NSView*) aView {
NSEnumerator* e;
NSView* aSubview;
NSView* result;
if ([aView isKindOfClass: [NSButton class]] &&
[[(NSButton*)aView title] isEqualToString: @"Cancel"])
return aView;
e = [[aView subviews] objectEnumerator];
while ((aSubview = [e nextObject]))
if ((result = [self seekCancelButton: aSubview]))
return result;
return nil;
}
This wouldn't work if the primary language was other than English
(dunno if that matters in Mark's case). Maybe there's a way to ask
Cocoa what string is used for the title of the OK button? Then that
could be passed to the method -- which could be added to a category
of NSView, by the way:
- (id)seekButtonWithTitle:(NSString *)title
{
// ...
}
...
For thoroughness, a variation could be added that would also find a
cell in an NSMatrix with the specified title.
Another approach I just thought of would be a variation of
-viewWithTag: that only looks for certain classes of view. By
relying on the tag (assuming the buttons are tagged as Mark believes)
this would avoid the internationalization problem. It could use
essentially Matt's recursive logic above but filter by tag instead of
title (again, in a category of NSView):
- (id)viewOfClass:(Class)aClass withTag:(int)tag
{
NSEnumerator* e;
NSView* aSubview;
NSView* result;
if (([self tag] == tag) && [self isKindOfClass:aClass])
return self;
e = [[self subviews] objectEnumerator];
while ((aSubview = [e nextObject]))
if ((result = [aSubview viewOfClass:aClass withTag:tag]))
return result;
return nil;
}
And you could have a convenience method that searches for buttons:
- (id)buttonWithTag:(int)tag
{
return [self viewOfClass:[NSButton class] withTag:tag];
}
Again, to get really thorough you could add a variation that looks
for cells in NSMatrixes.
Minor note: in the first "if (...)", I switched the order of the
tests because I assume checking the tag is faster than checking the
class, but in practice this almost certainly makes no meaningful
difference.
Disclaimer: I haven't compiled any of the above.
--Andy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.