Forum Navigation
Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: fbcocoaTutorial_2
You need to log in to create posts and topics.

Tutorial_2

Posted by: svanvoorst <svanvoorst@...>

This demo starts to add controls to the window and demonstrates a controller and delegate.  Three ways to quit the application are shown: quit menu, quit button, and window close button.  These are basic closure methods and would be more complex if we needed to check for changes which had been made by the user.

'---- start -----

BeginCDeclaration
@interface AppDelegate : NSObject
//methods automatically called
@end //interface
EndC
BeginCFunction
@implementation AppDelegate
// --- Taken from NSApplicationDelegate Protocol Reference ---- //
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *) theApp;
{
   return YES;
}
@end //implementation
EndC
BeginCDeclaration
@interface AppController : NSObject
{
}
// ---- Methods listed here ---- //
- (void) myBtnAction:(id)sender;
@end //interface
EndC
BeginCFunction
@implementation AppController
// --- Methods defined here ---- //
-(void) myBtnAction:(id)sender
{
  NSBeep();
}
@end //implementation
EndC
BeginCCode
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
 
  [NSApplication sharedApplication];
  [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// --- Menu bar and menus ---- //
   id menubar = [[NSMenu new] autorelease];
   id appMenuItem = [[NSMenuItem new] autorelease];
   [menubar addItem:appMenuItem];
   [NSApp setMainMenu:menubar];
   id appMenu = [[NSMenu new] autorelease];
   id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
        action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
   [appMenu addItem:quitMenuItem];
   [appMenuItem setSubmenu:appMenu];
// ---- Window ------- //
   #define _wndW  430
   #define _wndH  300
   id window = [ [ NSWindow alloc ] 
                    initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
                    styleMask: NSClosableWindowMask | NSTitledWindowMask
                    backing:NSBackingStoreBuffered
                    defer:NO];
   [window center];
   [window setTitle: @"Controller/Delegate_demo" ];
   [window makeKeyAndOrderFront:nil]; 
// Create an instance of AppDelegate (NSObject)
  id myDelegate = [[AppDelegate alloc]init];
  [NSApp setDelegate:myDelegate];
// Create an instance of AppController (NSObject)
  id myController = [[AppController alloc]init];
  NSButton*btn = [[NSButton alloc]initWithFrame:NSMakeRect( 100, _wndH - 100, 150, 30 )];
  [[window contentView] addSubview: btn];
  [btn setBezelStyle:NSRoundedBezelStyle ];
  [btn setTitle: @"Beep" ];
  [btn setTarget:myController];
  [btn setAction:@selector(myBtnAction:)];
  [btn release];
  NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 130, 30, 95, 30 )];
  [quitBtn setBezelStyle:NSRoundedBezelStyle ];
  [quitBtn setTitle: @"Quit" ];
  [quitBtn setAction:@selector(terminate:)];
  [[window contentView] addSubview: quitBtn];
  [quitBtn release];
  [NSApp activateIgnoringOtherApps:YES];
  [NSApp run];
  [pool drain];
  return NSApplicationMain(argc,  (const char **) argv);
}
EndC
'------ end -----
Steve Van Voorst


To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]