Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > cocoa without NIB file (need help)

cocoa without NIB file (need help)
Thread Tools
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Apr 17, 2004, 01:30 AM
 
im trying to create a very simple "test" cocoa application, without Xcode and IB, just to learn how to do it.
ive succeding in creating a window and adding a view, a textfield, and a button
i can move and resize the window using the mouse, and quit the application by clicking the button.

my problem is that i cant select the textview and type stuff in it. what happens is the application that was front application when i started my app(usually Terminal), still gets all key events, even after ive clicked on my apps window.

this is the code:

myView.h
Code:
#import <Cocoa/Cocoa.h> @interface myView : NSView { } @end
myView.m
Code:
#import "myView.h" @implementation myView - (void)drawRect:(NSRect)rect { NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(10,10,180,180)]; [[NSColor greenColor] set]; [path fill]; NSLog(@"myView: drawRect:"); } @end
main.m
Code:
#import <Cocoa/Cocoa.h> #import "myView.h" int main(int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *window; myView *view; view = [[myView alloc] initWithFrame:NSMakeRect(0,100,200,200) ]; window = [[NSWindow alloc] initWithContentRect:NSMakeRect(50,100,200,300) styleMask:NSTitledWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:TRUE]; NSTextField *text=[[NSTextField alloc]initWithFrame:NSMakeRect(10,60,180,22) ]; [text setStringValue:@"sample text"]; NSButton *button=[[NSButton alloc]initWithFrame:NSMakeRect(10,10,180,32) ]; [button setBezelStyle:NSRoundedBezelStyle]; [button setTitle:@"Quit"]; [button setTarget:NSApp]; [button setAction:@selector(terminate:)]; [window setTitle:@"test1"]; [[window contentView] addSubview:view]; [[window contentView] addSubview:text]; [[window contentView] addSubview:button]; [NSApplication sharedApplication]; [window makeKeyAndOrderFront: nil]; [pool release]; [NSApp run]; return 0; }
tarball of the source files: test.tgz


this is how i compile and run
Code:
cc -framework Cocoa myView.m main.m ./a.out
any help would be greatly appreciated :)
     
Junior Member
Join Date: Sep 2003
Location: Nowhere, Missouri
Status: Offline
Reply With Quote
Apr 17, 2004, 05:39 PM
 
Subclass NSWindow, and override the canBecomeKey method, like so:

- (BOOL)canBecomeKeyWindow
{
return YES;
}

That should fix it.
     
ul1984  (op)
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Apr 17, 2004, 10:14 PM
 
Originally posted by Arclite:
Subclass NSWindow, and override the canBecomeKey method, like so:

- (BOOL)canBecomeKeyWindow
{
return YES;
}

That should fix it.
thanks!

ill give it a try right away
     
ul1984  (op)
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Apr 17, 2004, 10:20 PM
 
no difference with the subclass of NSWindow

i have a feeling the thing missing has something to do with bringing up the menubar or dock icon, because when i start my app, all that happens is that the window pops up, it still looks as Terminal is my front application(the terminal Menubar is still there)
     
Junior Member
Join Date: Sep 2003
Location: Nowhere, Missouri
Status: Offline
Reply With Quote
Apr 17, 2004, 10:47 PM
 
Hrm. I know now what is happening, but I'm not sure how to fix it without Xcode. It seems like you should have LSUIElement (defined in Info.plist) set to something else. When it's set to 1, it behaves as it is now. That's how the joke app DeadScreen works.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Apr 18, 2004, 02:26 PM
 
You need to make it into a real application.

1. Create a folder called test.app from the commandline.
2. In the test.app directory, make a folder called Contents
3. In the Contents folder, make a folder called MacOS
4. In the MacOS folder, move your executable a.out file and name it test

Double click on test.app in the Finder and it will work.

Hope this helps,
Matt Fahrenbacher
     
ul1984  (op)
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Apr 18, 2004, 02:50 PM
 
Originally posted by Ghoser777:
You need to make it into a real application.

1. Create a folder called test.app from the commandline.
2. In the test.app directory, make a folder called Contents
3. In the Contents folder, make a folder called MacOS
4. In the MacOS folder, move your executable a.out file and name it test

Double click on test.app in the Finder and it will work.

Hope this helps,
Matt Fahrenbacher
are you sure that's the only way?

i dont really need the dock icon and menubar, being able to make the window active so that i could type in it, would suffice.

i thought maybe in a standard app, the NSApplicationMain() function runs a few functions that will read the NIB file and the Info.plist and setup the menubar and the dock icon, should be possible to do that manually just as well? but maybe its not

i think it would be really interested to know how, but i havent been able to find any example code that does this.


anyway guys, thanks for trying to help
     
ul1984  (op)
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Apr 18, 2004, 03:28 PM
 
Originally posted by ul1984:

i thought maybe in a standard app, the NSApplicationMain() function runs a few functions that will read the NIB file and the Info.plist and setup the menubar and the dock icon, should be possible to do that manually just as well?
i guess i didnt think that through very well, i tested you're suggestion with creating the test.app and it worked, so i guess that means NSApplicationMain() doesnt really do anything special
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2