• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Writing Cocoa apps w/o using Interface Builder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing Cocoa apps w/o using Interface Builder


  • Subject: Re: Writing Cocoa apps w/o using Interface Builder
  • From: Gwynne Raskind <email@hidden>
  • Date: Sat, 30 May 2009 14:09:42 -0400

On May 30, 2009, at 1:01 PM, Jonathan Mast wrote:
Everyone,
Could someone please post a "HelloCocoa" equivalent to this Java program:
<code>
package example;


import javax.swing.*;

public class HelloJava {
   public static void main(String[] args) {
       JFrame jf = new JFrame();
       JPanel jp = new JPanel();
       jp.add(new JButton("Hello Java!"));
       jf.setContentPane(jp);
       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jf.pack();
       jf.setVisible(true);
   }
}
</code>

The only this program does is create a window with a clickable button in
it.


Okay, I've got an hour to kill, I'll give it a shot :). Strangely, it's worth noting that the iPhone (Cocoa Touch) version of this is slightly simpler than the OS X (plain Cocoa) version in some places, but oh well. The code below works... more or less. It'll present the window in question with a button in it, but the UI is frozen - no drawing takes place on interaction with the objects. Why? Because there's some kind of arcane incantation needed (probably a main menu) to make NSApplication work. In short, it's much more complex to do a nibless Cocoa app than whatever the Java equivelant is.

// main.m
@interface HelloCocoaApp : NSObject
{
	NSWindow	*window;
	NSButton	*button;
}
@end

@implementation HelloCocoaApp
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
	NSRect		windowRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
	NSRect		buttonRect = NSMakeRect(0.0, 0.0, 50.0, 32.0);

	window = [[NSWindow alloc] initWithContentRect:windowRect
		styleMask:NSTitledWindowMask | NSClosableWindowMask
		backing:NSWindowBackingLocationDefault
		defer:NO];
	[window center];
	[window setDelegate:self];

	button = [[NSButton alloc] initWithFrame:buttonRect];
	[button setTitle:@"Hello Cocoa!"];
	[button setButtonType:NSMomentaryLightButton];

	[[window contentView] addSubview:button];

	[window makeKeyAndOrderFront:self];
}

- (void)windowWillClose:(NSNotification *)notification
{
	[NSApp terminate:self];
}
@end

int main(int argc, char **argv)
{
	NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];

	[NSApplication sharedApplication];
	[NSApp setDelegate:[[[HelloCocoaApp alloc] init] autorelease]];
	[NSApp run];
	[pool release];
	return 0;
}

-- Gwynne

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Writing Cocoa apps w/o using Interface Builder
      • From: Bill Bumgarner <email@hidden>
References: 
 >Writing Cocoa apps w/o using Interface Builder (From: Jonathan Mast <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: Chris Williams <email@hidden>)
 >Fwd: Writing Cocoa apps w/o using Interface Builder (From: Jonathan Mast <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: Seth Willits <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: Jonathan Mast <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: Finlay Dobbie <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: colo <email@hidden>)
 >Re: Writing Cocoa apps w/o using Interface Builder (From: Jonathan Mast <email@hidden>)

  • Prev by Date: Re: Writing Cocoa apps w/o using Interface Builder
  • Next by Date: Re: Writing Cocoa apps w/o using Interface Builder
  • Previous by thread: Re: Writing Cocoa apps w/o using Interface Builder
  • Next by thread: Re: Writing Cocoa apps w/o using Interface Builder
  • Index(es):
    • Date
    • Thread