outlet works in awakeFromNib but not from function

Discussion in 'Mac Programming' started by justmyself, Jan 7, 2009.

  1. justmyself macrumors member

    Joined:
    Jan 2, 2009
    #1
    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.
     
  2. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #2
    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];
     
  3. justmyself thread starter macrumors member

    Joined:
    Jan 2, 2009
    #3
    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.
     
  4. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    Mixing UIImage and NSImage doesn't sound like it would work...
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    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?
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. justmyself thread starter macrumors member

    Joined:
    Jan 2, 2009
    #7
    backgroundImage is an outlet for NSImageView.

    :confused:
     
  8. BorgCopyeditor macrumors newbie

    Joined:
    Jan 7, 2009
    #8
    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
     
  9. justmyself thread starter macrumors member

    Joined:
    Jan 2, 2009
    #9
    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.
     
  10. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #10
    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.
     
  11. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #11
    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
    
     
  12. justmyself thread starter macrumors member

    Joined:
    Jan 2, 2009
    #12
    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
     

Share This Page