Hello !

I don't know if this got answered but here is my attempt. It is just a basic way of programmatically setting an object as a application delegate and programmatically creating a button on the applications main
window and setting it to perform an action when clicked.

I know that there are some issues about the way this is implemented but it points the way to go.
Since I am a newbie my self any suggestions are more than welcome.

Brief explanation follows.

In Interface Builder:
- in MainMenu.xib create generic NSObject by adding it from the Library
- select that NSObject and in the Identity Inspector make it an instance of our ApplicationController class
In Xcode:
- in awakeFromNib we make our self as the application delegate
- in applicationDidFinishLaunching: we tell the application to make the first window that it finds as main
window, because at this time there is still no main window
- then we create our button instance, set its action as a selector with our action method, set our self as a target of that action, and perform all necessary initializations for our button (title, type, etc)
- we add our button on the main window and then release the button
- in dealloc we remove our self as being application delegate
- okButtonAction: is our button's action method that performs NSBeep() function

Code follows.


ApplicationController.h

#import <Cocoa/Cocoa.h>


@interface ApplicationController : NSObject
{
        
}

@end

ApplicationController.m

#import "ApplicationController.h"


@implementation ApplicationController

- (void)awakeFromNib
{
        [NSApp setDelegate:self];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
        [NSApp makeWindowsPerform:@selector(makeMainWindow) inOrder:NO];
        
NSButton * okButton = [[NSButton alloc] initWithFrame:NSMakeRect(10.0, 10.0, 96.0, 32.0)];
        [okButton setAction:@selector(okButtonAction:)];
        [okButton setTarget:self];
        [okButton setTitle:@"OK"];
        [okButton setButtonType:0];
        [okButton setBezelStyle:11];
        
        [[[NSApp mainWindow] contentView] addSubview:okButton];
        [okButton release];
}

- (void)dealloc
{
        [NSApp setDelegate:nil];
        
        [super dealloc];
}

- (void)okButtonAction:(id)sender
{
        NSBeep();
}

@end


Thanks.


Mario Kušnjer
mario.kusn...@sb.t-com.hr
+385957051982
mariokusnjer (at) Skype



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to