 |
 |
cocoa without NIB file (need help)
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status:
Offline
|
|
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
|
|
Subclass NSWindow, and override the canBecomeKey method, like so:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
That should fix it.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status:
Offline
|
|
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 
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status:
Offline
|
|
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 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|