Opening XIB on Button Click?

Discussion in 'Mac Programming' started by Yellowstone2012, Jul 12, 2011.

  1. Yellowstone2012, Jul 12, 2011
    Last edited: Jul 12, 2011

    Yellowstone2012 macrumors regular

    Feb 3, 2011
    How would I open a XIB on a button click? I have tried creating a window via IB, and using makeKeyAndOrderFront doesn't display my window. So, how would I open a window from a XIB?


    (I'm Using Xcode 4.0.2)

    I tried to use isVisible with myWindow, but XCode says the isVisible method not found (id).
  2. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    Create a XIB with a window and a controller to go with it. In my code example, it was called 'SubtimerWindow.xib'. Add the XIB to the resources section of your application. I connected a button on my main window to this method call in the main window controller to create the secondary window and its controller. subTimerList is an NSArray in my main window controller class.

    - (IBAction)makeNewTimer:(id)sender {
        NSNib *nnib = [[NSNib alloc] initWithNibNamed:@"SubtimerWindow" bundle:nil];
        NSArray *topLevelObjects;
        BOOL result;
        result = [nnib instantiateNibWithOwner:self topLevelObjects:&topLevelObjects];
        if (result)
            [subTimerList addObject:topLevelObjects];
        [nnib release];
  3. Yellowstone2012 thread starter macrumors regular

    Feb 3, 2011
    Thanks. I've found an easier way to open windows from XIBs.

    my.h file:

    @interface MainMenu : NSObject {
        IBOutlet NSButton *buttonOne;
        IBOutlet NSWindow *window2;
        NSWindowController *myControl;
    @property (nonatomic, retain) IBOutlet NSButton *buttonOne;
    @property (nonatomic, retain) IBOutlet NSWindow *window2;
    - (IBAction)openWindow:(id)sender;
    my.m file:

    - (IBAction)openWindow:(id)sender {
        myControl = [[NSWindowController alloc] initWithWindowNibName:@"Window2"];
        [myControl showWindow:self];
  4. jiminaus macrumors 65816


    Dec 16, 2010
    So you're initial question seems to have been incorrect. You didn't actually want to open a XIB, you just wanted to open a 2nd window in the same XIB.
  5. Yellowstone2012 thread starter macrumors regular

    Feb 3, 2011
    You can also use the NSWindowController to open windows in the same XIB? I didn't know that. I was using the NSWindowController to open a window from another XIB.

    So, all I really need is one XIB for an apps entire interface, correct?


Share This Page