1

Here is my code:

#import <Cocoa/cocoa.h> #import <AppKit/AppKit.h> @interface OGLView: NSOpenGLView{} @end @implementation OGLView - (void)keyDown:(NSEvent *)event { NSLog(@"Hi there");//never called } - (BOOL)acceptsFirstResponder{return YES;} - (BOOL)becomeFirstResponder{return YES;} - (BOOL)resignFirstResponder{return YES;} - (BOOL)canBecomeKeyView { return YES; } @end int main( int argc, char* args[] ){ NSWindow *win = nil; NSRect e = [[NSScreen mainScreen] frame]; win = [ [NSWindow alloc] initWithContentRect: e styleMask: NSTitledWindowMask |NSClosableWindowMask |NSMiniaturizableWindowMask backing: NSBackingStoreBuffered defer: NO ]; view =[[[OGLView alloc] initWithFrame:e] autorelease]; [win orderFrontRegardless]; [win setReleasedWhenClosed:YES]; [win setContentView:view]; [win setInitialFirstResponder:view]; [win setNextResponder:view]; [win makeFirstResponder:view]; [win setAcceptsMouseMovedEvents:YES]; [view setNeedsDisplay:YES]; [view display]; } 

Does I must create NSResponder subcalss? Or NSCOntroller? How can I connect this subclasses to my OGLView? Please help.. I'm newbie in Objective-C. Also I'm programming on Eclipse (not Xcode)

1 Answer 1

3

You can't put this code in main(). There's no application object, so there's no connection to the window server. There's no event loop. Frankly, as the code stands, your program will exit immediately because there's nothing preventing execution from falling off the end of main().

Your main() should call NSApplicationMain(). Ideally, your app should be properly bundled with an Info.plist file and a MainMenu NIB. Loading that NIB would instantiate an instance of a controller class of your design and designate it as the application object's delegate. Then, you would put early start-up code like yours in the app delegate's -applicationDidFinishLaunching: method.

If for some reason you refuse to use NIBs, you can call [NSApplication sharedApplication] in your main(). Instantiate your controller class and directly assign it as the application object's delegate using [NSApp setDelegate:yourObject]. Then call [NSApp run]. Again, further initialization should be done in the delegate's methods.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.