make connections without InterfaceBuilder

On 2010.02.09, at 08:33, Ian Jackson wrote:

Related Posts

  1. Mario Ku says:

    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

    @interface ApplicationController : NSObject {

    }

    @end

    ApplicationController.m

    #import “ApplicationController.h”

    @implementation ApplicationController

    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]; }

    @end

    Thanks.

    Mario Ku

  1. There are no trackbacks for this post yet.

Leave a Reply