How correctly to use my class in main.m?

Discussion in 'Mac Programming' started by DeDMakar, Mar 31, 2008.

  1. macrumors member

    Joined:
    Mar 24, 2008
    #1
    How correctly to use my class in main.m? Project - Cocoa Application.
    Class:
    Code:
    //  "InitFormApp.h"
    
    #import <Cocoa/Cocoa.h>
    
    @interface InitFormApp : NSObject {
    IBOutlet NSTextField *textfield;
    IBOutlet NSImageView *image;
    }
    -(id)InitForm;
    @end

    Code:
    // "InitFormApp.m"
    
    #import "InitFormApp.h"
    
    @implementation InitFormApp
    -(id)InitForm
    {
    	[super init];
    	[image setImage:[NSImage imageNamed:@"apple_big.jpg"]];
    	[textfield setStringValue:[NSString stringWithFormat:@"User %@", NSUserName()]];
    	return self;
    }
    @end
    Code:
    //  "main.m"
    
    
    #import <Cocoa/Cocoa.h>
    #import "InitFormApp.h"
    
    int main(int argc, char *argv[])
    {
        InitFormApp *initus = [InitFormApp InitForm]; // [COLOR="Red"]How it is correct????[/COLOR]
        return NSApplicationMain(argc,  (const char **) argv);
    }
    I want that method InitForm was processed at start of the project(application).
    Thanks...
     
  2. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    Take a look at the Example projects in Developer > Examples > AppKit.

    The SimpleTemperatureConverter project is a good starting point. And then look at SimpleBrowser.
     
  3. thread starter macrumors member

    Joined:
    Mar 24, 2008
    #3
    I must use initialize method of NSObject? But this method can not use IBOutlet and other variables, who are registered ".h" file.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    In your MainMenu.nib, you need to create an instance of your InitFormApp object there and connect your IBOutlets. Back in your code, remove your line of code in main.m, and move your code from InitForm to awakeFromNib.

    You may also want to read The Nib Object Life Cycle.
     
  5. thread starter macrumors member

    Joined:
    Mar 24, 2008
    #5
    I use method awakeFromNib.
    Code:
    -(void)awakeFromNib
    {
    	[image setImage:[NSImage imageNamed:@"apple_big.jpg"]];
    	[textfield setStringValue:[NSString stringWithFormat:@"User: %@", NSUserName()]];
    }
     

Share This Page