Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Yellowstone2012

macrumors regular
Original poster
Feb 3, 2011
108
0
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?

Thanks.

(I'm Using Xcode 4.0.2)

I tried to use isVisible with myWindow, but XCode says the isVisible method not found (id).
 
Last edited:
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.

Code:
- (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];
}
 
Thanks. I've found an easier way to open windows from XIBs.

my.h file:

Code:
@interface MainMenu : NSObject {
@private
    
    IBOutlet NSButton *buttonOne;
    IBOutlet NSWindow *window2;
    NSWindowController *myControl;
    
    
    
}

@property (nonatomic, retain) IBOutlet NSButton *buttonOne;
@property (nonatomic, retain) IBOutlet NSWindow *window2;

- (IBAction)openWindow:(id)sender;


@end

my.m file:

Code:
- (IBAction)openWindow:(id)sender {
    
    myControl = [[NSWindowController alloc] initWithWindowNibName:@"Window2"];
    [myControl showWindow:self];
    
}

Source: http://www.computermusicresource.com/xcode/openawindow.html
 
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.

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?

Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.