Calling a routine from another module
G'day All,
So many painful little problems.
if my module looks like this:
#import "controller.h"
@implementation controller
-(void)PrintHello
{
NSLog(@"I got this far");
[txtFloatIn setStringValue:[NSString stringWithString:@"Hello"]];
}
-(IBAction)PrintFloat:(id)sender
{
float myVar;
myVar = [txtFloatIn floatValue];
[txtStringOut setStringValue:[NSString stringWithFormat:@"The number is %f",myVar]];
//[self PrintHello];
}
@end
What do I put in the appDelegate to call PrintHello on start up.
I thought [controller PrintHello] would do it since [self PrintHello] works from within the controller module.
BTW the appDelegate.m looks like this:
#import "Float_to_StrAppDelegate.h"
#import "controller.h"
@implementation Float_to_StrAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSLog(@"The delegate ran");
[controller PrintHello];
}
@end
When I run this app the NSLog reports when the appDelegate runs but then I get an error:
+[controller PrintHello]: unrecognized selector sent to class 0x100002160
All help gratefully accepted. I've been trawling the net for an hour trying solve this.
Cheers,
A.
Well the leading "+" means "class method" to Objective-C and the "-" is an instance method. The message you sent was to the class. In the applicationDidFinishLaunching: method, you have to make sure you're sending it to an instance of controller.
I would do this in the awakeFromNib: handler instead of applicationDidFinishLaunching: because what if the nib wasn't loaded?
You'll have to say:
[controller PrintHello:nil];
because, you've declared PrintHello:(id) sender;
I've written the code for you: http://clanmills.com/articles/cocoatutorials/AHansen.zip
I share your frustration with Cocoa. I also found it puzzling at first. However, trust me, you will get to like this. I've written some tutorials: http://clanmills.com/articles/cocoatutorials/ which I hope will help make the learning less painful.