Re: display an image in a button
Re: display an image in a button
- Subject: Re: display an image in a button
- From: email@hidden
- Date: Wed, 18 Feb 2004 02:21:18 EST
Hi Louis,
unfortunately, it's defined as an NSObject class and not a
NSPreferencePane class.
I understand now that the Application is considered the main bundle
however, when the preferencePane BNDL obtains the CFBundleVersion it was obtaining
it from the preferencePane and I had to specify to get it from the main bundle:
NSString *currVersionNumber = [[[NSBundle
bundleWithIdentifier:@"com.LogiK.XSKeyEMU"]
infoDictionary] objectForKey:
@"CFBundleVersion"];
so why does it select itself in some instances and the main bundle in others.'
Would it not make more sense if it obtained everything from the the main
bundle or the preferencePane bundle?
How can it be selective and what decides where information comes from?
Obviously your first approach solves the problem if I really need it to load
the images from the bundle but is their any reason why I shouldn't do this or
is this an acceptable practice?
To make your suggestion a little more transportable, I would assume that this
is valid
NSString *imagePath = [[NSBundle bundleForClass:[self class]]
pathForImageResource:some_image];
and will always load the image from the calling bundles resource folder?
Back to my first problem, why did the missing images from the main bundle not
through up some kind of flag?
I would have thought that not being able to load an image because it's
missing would generate some kind of error but at no time did any error appear in the
run window.
While I am defined as NSObject <SS_PreferencePaneProtocol> and
PreferencePanes.framework is not imported or linked in any files, can my class still be
considered a subclass of NSPreferencePane?
Well, enough questions for now, I'm still chewing your response as it
inspires thoughts and questions
Dale
In a message dated 02/17/2004 23:12:56, email@hidden writes:
>
Hello...
>
>
Instead of using the NSImage class method imageNamed:, you probably
>
need to load the images from the bundle manually. The problem in your
>
case seems to be that the images are not found since NSImage only
>
looks in the main bundle for images when you use imageNamed:... In
>
the case of a preference pane, the main bundle is the System
>
Preferences application, not your preference pane bundle, so it
>
doesn't find your images.
>
>
>
In other words, instead of [lockButton setImage:[NSImage
>
imageNamed:LOCK_IMG]], you would do:
>
>
NSString *imagePath = [[NSBundle
>
bundleForClass:[YourPreferencePaneClass class]]
>
pathForImageResource:LOCK_IMG];
>
[lockButton setImage:[[[NSImage alloc]
>
initWithContentsOfFile:imagePath] autorelease]];
>
>
>
In this case, if your code is in a NSPreferencePane subclass, you can
>
using the NSPreferencePane instance method bundle and simply do this:
>
>
NSString *imagePath = [[self bundle] pathForImageResource:LOCK_IMG];
>
[lockButton setImage:[[[NSImage alloc]
>
initWithContentsOfFile:imagePath] autorelease]];
>
>
>
The other alternative is to load those images when your preference
>
pane is loaded, and register them under the names you want to use
>
(although in order to prevent problems, you would likely want to
>
prefix the names somehow to avoid naming conflicts, for example with
>
the name of the class as in the example below, and you would have to
>
rename the image files to match) If you did it this way, you would
>
leave your toggleAuthorization method the way it is now, and just
>
register the images in the initializer and release them in dealloc.
>
Building on your existing example:
>
>
- - - file: YourPreferencePaneClass.m - - -
>
>
/* ... defines for lock and unlock messages ... */
>
>
#define LOCK_IMG @"YourPreferencePaneClass_lock"
>
#define UNLOCK_IMG @"YourPreferencePaneClass_unlock"
>
>
>
@implementation YourPreferencePaneClass.m
>
>
- (id)initWithBundle:(NSBundle *)bundle
>
{
>
if (self = [super initWithBundle:bundle])
>
{
>
/* ... your current init stuff ... */
>
>
NSImage *lockImage = [[NSImage alloc]
>
initWithContentsOfFile:[bundle pathForImageResource:LOCK_IMG]];
>
[lockImage setName:LOCK_IMG];
>
NSImage *unlockImage = [[NSImage alloc]
>
initWithContentsOfFile:[bundle pathForImageResource:UNLOCK_IMG]];
>
[unlockImage setName:UNLOCK_IMG];
>
}
>
>
return self;
>
}
>
>
/* ... the rest of your methods, including toggleAuthorization: ... */
>
>
- (void)dealloc
>
{
>
/* ... your current dealloc stuff ... */
>
>
[[NSImage imageNamed:LOCK_IMG] release];
>
[[NSImage imageNamed:UNLOCK_IMG] release];
>
>
[super dealloc];
>
}
>
>
@end
>
- - - - - - - - - - -
>
>
>
Hope that helps,
>
>
Louis
>
>
>
>
>In a message dated 02/17/2004 00:12:56, email@hidden writes:
>
>_______________________________________________
>
>
>
>>Information:
>
>>_______________________________________________
>
>>
>
>>from header
>
>>_______________________________________________
>
>>
>
>>//Titles and Messages
>
>>#define LOCK_MSG @"Click to allow changes"
>
>>#define UNLOCK_MSG @"Click to disallow changes"
>
>>
>
>>//Button Images
>
>>#define LOCK_IMG @"lock"
>
>>#define UNLOCK_IMG @"unlock"
>
>>_______________________________________________
>
>>
>
>>from source
>
>>_______________________________________________
>
>>
>
> >- (IBAction)toggleAuthorization:(id)sender
>
>>{
>
>> if([[Authorize sharedInstance]isAuthenticated])
>
>> {
>
>> [[Authorize sharedInstance]deauthenticate];
>
>> [applyButton setEnabled:NO];
>
>> [myPopUp setEnabled:NO];
>
> > [lockButton setImage:[NSImage imageNamed:LOCK_IMG]];
>
>> [authLabel setStringValue:LOCK_MSG];
>
>> }
>
>> else
>
>> {
>
>> if([[Authorize sharedInstance]authenticate])
>
>> {
>
>> [applyButton setEnabled:YES];
>
>> [myPopUp setEnabled:YES];
>
>> [lockButton setImage:[NSImage imageNamed:UNLOCK_IMG]];
>
>> [authLabel setStringValue:UNLOCK_MSG];
>
>> }
>
> > }
>
> >_______________________________________________
_______________________________________________
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.