Cocoa In XCode: How can I use two .xib files in 1 project?

Discussion in 'Mac Programming' started by rebello95, Nov 26, 2011.

  1. macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #1
    Hey. I'm trying to figure out how to make it so that when someone clicks a button, it closes the window that was currently open, then opens a new window in the same application. How can I do this?

    Thanks!
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Start reading the NSWindowController documentation.
     
  3. Senor Cuete, Dec 6, 2011
    Last edited: Dec 6, 2011

    macrumors regular

    Joined:
    Nov 9, 2011
    #3
    Notifications

    Since you mentioned two .xib files in your project I assume that you need to communicate between two .xibs. You can't establish a connection between two of these files using IBOutlets, etc. The way to do this is to use notifications. For example in the object methods of the first .xibs file owner, when the window is closed post a notification:

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName: @"firstWindowHasClosed" object: nil]; //post notification so other window knows to open

    And in the other file owner object that controls the other window:

    - (id) init //override class init so you can register it as an observer
    {
    if(self = [super init]) {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver: self selector: @selector(openNewWindow: ) name: @"firstWindowHasClosed" object: nil];

    Then when your first window posts the notification the second object receives the notification and calls the openNewWindow method. Be sure to call:
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver: self];

    in the object's dealloc method.
     
  4. thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #4
    Thanks!
     
  5. macrumors regular

    Joined:
    Nov 9, 2011
    #5
    Also it's important to mention that notifications happen right away. When you post a notification the listeners are notified immediately. They don't wait for the event cycle.
     

Share This Page