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

    Joined:
    Feb 3, 2011
    #1
    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).
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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];
    }
    
     
  3. Yellowstone2012 thread starter macrumors regular

    Joined:
    Feb 3, 2011
    #3
    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
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    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

    Joined:
    Feb 3, 2011
    #5
    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.
     

Share This Page