Re: display an image in a button
Re: display an image in a button
- Subject: Re: display an image in a button
- From: "Louis C. Sacha" <email@hidden>
- Date: Tue, 17 Feb 2004 20:12:17 -0800
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.