Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

justmyself

macrumors member
Original poster
Jan 2, 2009
32
0
I wrote a function to set a background image. The code compiles and works fine if it's in the awakeFromNIB function but not when the code is moved to a function.

The error is:

error: 'backgroundImage' undeclared (first use in this function)

Here is the code.

void setBackgroundImage()
{
NSImage *bgimage = [NSImage imageNamed:mad:"image.jpg"];
[backgroundImage setImage:bgimage];
}

Any help would be appreciated.
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
I wrote a function to set a background image. The code compiles and works fine if it's in the awakeFromNIB function but not when the code is moved to a function.

The error is:

error: 'backgroundImage' undeclared (first use in this function)

Here is the code.

void setBackgroundImage()
{
NSImage *bgimage = [NSImage imageNamed:mad:"image.jpg"];
[backgroundImage setImage:bgimage];
}

Any help would be appreciated.

Um. because backgroundImage isn't declared in your function.

You need to pass the backgroundImage variable to the function.


- (void) setBackgroundImage:(UIImage *)backgroundImage
{
NSImage *bgimage = [NSImage imageNamed:mad:"image.jpg"];
[backgroundImage setImage:bgimage];
}

and then call it with

[self setBackgroundImage:backgroundImage];
 

justmyself

macrumors member
Original poster
Jan 2, 2009
32
0
Still not working. argh...

I'm getting an error on the first line, it reads, Parse Error before'UIImage'

is the changed code

- (void) setBackgroundImage:(UIImage *)backgroundImage
{
NSImage *bgimage = [NSImage imageNamed:mad:"image.jpg"];
[self setImage:bgimage];
}

Your help is much appreciated.

Thank you.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
Yeah, that shouldn't be a UIImage* and also, in the revised Objective-C method, you never actually use the passed-in variable called "backgroundImage". The imageNamed: method should be used outside of the method and before calling it to get the image, and that image passed to the method. It's kind of odd because it looks like you sort of have an accessor that calls another accessor to do the same thing. What is backgroundImage? An NSImage? NSImageView?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
backgroundImage is (I'm assuming) an instance variance and ivars can only be directly accessed by instance methods. A function is not an instance method and cannot access ivars.
 

justmyself

macrumors member
Original poster
Jan 2, 2009
32
0
What is backgroundImage? An NSImage? NSImageView?

backgroundImage is an outlet for NSImageView.

The imageNamed: method should be used outside of the method and before calling it to get the image, and that image passed to the method. It's kind of odd because it looks like you sort of have an accessor that calls another accessor to do the same thing.

:confused:
 

BorgCopyeditor

macrumors newbie
Jan 7, 2009
14
0
backgroundImage is an outlet for NSImageView.

The reason your function is not working is that backgroundImage is not in scope. It's not "visible" from within your function, which is what the compiler is telling you with the word "undeclared": it has never seen anything called backgroundImage, and so doesn't know what to do with it.

As another poster pointed out, this is probably because backgroundImage is an instance variable, which means it's only visible in instance methods (and not in mere functions). awakeFromNib is such a method, which is why the code works there.

The revised version that you've typed in looks alright. Make sure you declare it in your header file, too.

HTH
 

justmyself

macrumors member
Original poster
Jan 2, 2009
32
0
Sorry if I'm missing the obvious, I bit new to Obj-C and Cocoa. Here is my header file. I think it's declared at line 5: IBOutlet NSImageView *backgroundImage; no?

/* Foo */

#import <Cocoa/Cocoa.h>

@interface Foo : NSObject
{
IBOutlet NSImageView *backgroundImage;
}
@end

Here is my main file

#import "Foo.h"

@implementation Foo

IBOutlet NSImageView *backgroundImage;
void setBackgroundImage();

- (void)awakeFromNib
{
setBackgroundImage();
}

- (void) setBackgroundImage:(UIImage *)backgroundImage
{
NSImage *bgimage = [NSImage imageNamed:mad:"image.jpg"];
[self setImage:bgimage];
}

@end

Now I get the error, parse error before UIImage. This is insane. I appreciate all your help guys.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
The syntax for calling an Obj-C method is:

Code:
[self setBackgroundImage];

not

Code:
setBackgroundImage();

And the method should be:

Code:
- (void) setBackgroundImage
{
  NSImage *bgimage = [NSImage imageNamed:@"image.jpg"];
  [backgroundImage setImage:bgimage];
}

Ideally, you'd pass the image name in as a parameter to make it more flexible, but the above should work.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
There's other stuff wrong as well.

Code:
#import <Cocoa/Cocoa.h>

@interface Foo : NSObject
{
    IBOutlet NSImageView *backgroundImage;
}

- (void) setBackgroundImage;

@end

Here is my main file

#import "Foo.h"

@implementation Foo
	
- (void)awakeFromNib
{		
     [self setBackgroundImage];
}

- (void) setBackgroundImage
{
     NSImage *bgimage = [NSImage imageNamed:@"image.jpg"];
     [backgroundImage setImage:bgimage];
}

@end
 

justmyself

macrumors member
Original poster
Jan 2, 2009
32
0
Just wanted to say thanks for everyones help :)

To help me on my way, where can I find more information of functions and passing variables?

Thanks so much
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.