PDA

View Full Version : How correctly to use my class in main.m?




DeDMakar
Mar 31, 2008, 04:42 AM
How correctly to use my class in main.m? Project - Cocoa Application.
Class:
// "InitFormApp.h"

#import <Cocoa/Cocoa.h>

@interface InitFormApp : NSObject {
IBOutlet NSTextField *textfield;
IBOutlet NSImageView *image;
}
-(id)InitForm;
@end


// "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

// "main.m"


#import <Cocoa/Cocoa.h>
#import "InitFormApp.h"

int main(int argc, char *argv[])
{
InitFormApp *initus = [InitFormApp InitForm]; // How it is correct????
return NSApplicationMain(argc, (const char **) argv);
}

I want that method InitForm was processed at start of the project(application).
Thanks...



Sayer
Mar 31, 2008, 08:30 AM
Take a look at the Example projects in Developer > Examples > AppKit.

The SimpleTemperatureConverter project is a good starting point. And then look at SimpleBrowser.

DeDMakar
Apr 2, 2008, 03:53 AM
Take a look at the Example projects in Developer > Examples > AppKit.

The SimpleTemperatureConverter project is a good starting point. And then look at SimpleBrowser.

I must use initialize method of NSObject? But this method can not use IBOutlet and other variables, who are registered ".h" file.

kainjow
Apr 2, 2008, 08:09 AM
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 (http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/chapter_3_section_3.html).

DeDMakar
Apr 4, 2008, 05:08 AM
I use method awakeFromNib.
-(void)awakeFromNib
{
[image setImage:[NSImage imageNamed:@"apple_big.jpg"]];
[textfield setStringValue:[NSString stringWithFormat:@"User: %@", NSUserName()]];
}