开发者

Cocoa: Calling a method from AppDelegate.m

开发者 https://www.devze.com 2022-12-17 22:52 出处:网络
In order to better understand the startup, event queue, and methods within my application I\'m trying to write a program that does two things: Play a beep at the startup and every time the user hits a

In order to better understand the startup, event queue, and methods within my application I'm trying to write a program that does two things: Play a beep at the startup and every time the user hits a button. So far it only plays when the user hits the button. I know there may be multiple ways to get the startup beep to play, but in order to work with initialization code I want to do it by calling my beep method from within the applicationDidFinishLaunching method of the AppDelegate.m file.

Here is my code:

Log.h

#import <Cocoa/Cocoa.h>


@interface Log : NSObject {

    IBOutlet id button;

}
-(void)beepAndLog;
-(IBAction)buttonPressed:(id)sender;

@end

L开发者_开发问答og.m

#import "Log.h"


@implementation Log

-(void)beepAndLog {

    NSLog(@"The Method Was Called!");
    NSBeep();

}

-(IBAction)buttonPressed:(id)sender {

    [self beepAndLog];
}
@end

And the applicationDidFinishLaunching method looks like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    [Log beepAndLog];

}

In the applicationDidFinishLaunching method, however, XCode warns me that

'Log' may not respond to '+beepAndLog'

and indeed, there is no beep and the log reads as follows:

MethodResponse[11401:a0f] +[Log beepAndLog]: unrecognized selector sent to class 0x100002100

("MethodResponse" is the name of my project, btw)

I'm unsure why Log wouldn't respond to beepAndLog, seeing as that's one of its methods. Am I calling it incorrectly? I have a feeling this will be painfully obvious to you more experienced people. I'm a newbie. Any help would be appreciated! Thanks!


There are two possibilities. Either you defined beepAndLog as an instance method, when you wanted a class method, or you want to call it on an instance when you called it on the class.

To change it to a class method, change the header to read:

+(void)beepAndLog;

and the implementation:

+(void)beepAndLog {
    NSLog(@"The Method Was Called!");
    NSBeep();
}

For the other solution, make sure you have an instance of class Log around (probably a singleton), and do something like:

[[Log logInstance] beepAndLog];

from your notification method. The Log class would need to look something like this:

Log.h:

#import <Cocoa/Cocoa.h>

@interface Log : NSObject {
    IBOutlet id button;
}

+(Log *)logInstance;

-(void)beepAndLog;
-(IBAction)buttonPressed:(id)sender;

@end

Log.m:

#import "Log.h"

Log *theLog = nil;

@implementation Log

+(Log *)logInstance
{
    if (!theLog) {
        theLog = [[Log alloc] init];
        // other setup (like hooking up that IBAction)
    }
    return theLog;
}

-(void)beepAndLog {
    NSLog(@"The Method Was Called!");
    NSBeep();
}

-(IBAction)buttonPressed:(id)sender {
    [[Log logInstance] beepAndLog];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消