Note that there are some explanatory texts on larger screens.

plurals
  1. POCocoa: Calling a method from AppDelegate.m
    text
    copied!<p>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. </p> <p>Here is my code:</p> <p>Log.h</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface Log : NSObject { IBOutlet id button; } -(void)beepAndLog; -(IBAction)buttonPressed:(id)sender; @end </code></pre> <p>Log.m</p> <pre><code>#import "Log.h" @implementation Log -(void)beepAndLog { NSLog(@"The Method Was Called!"); NSBeep(); } -(IBAction)buttonPressed:(id)sender { [self beepAndLog]; } @end </code></pre> <p>And the applicationDidFinishLaunching method looks like this:</p> <pre><code>- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application [Log beepAndLog]; } </code></pre> <p>In the applicationDidFinishLaunching method, however, XCode warns me that </p> <blockquote> <p>'Log' may not respond to '+beepAndLog'</p> </blockquote> <p>and indeed, there is no beep and the log reads as follows:</p> <blockquote> <p>MethodResponse[11401:a0f] +[Log beepAndLog]: unrecognized selector sent to class 0x100002100</p> </blockquote> <p>("MethodResponse" is the name of my project, btw)</p> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload