Cocoa: Calling Nibs

Discussion in 'Mac Programming' started by MacDonaldsd, Sep 17, 2007.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    I have 2 separate Nib files that I want both to be able to call a third nib file.

    The issue Im having is that it creates a separate instance of the third nib file for each and id like just the 1 Window which both can call.

    What is the best way to go about fixing this ? Global Variable ?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Yes, global or static variable. I usually do something like this in the nib's controller:

    Code:
    + (id)sharedController
    {
    	static id instance = nil;
    	if (!instance)
    		instance = [[[self class] alloc] init];
    	return instance;
    }
    
    - (id)init
    {
    	if (self = [super initWithWindowNibName:@"MyWindow" owner:self])
    	{
    	}
    	return self;
    }
    So let's say the class is MyWindowController (subclass of NSWindowController). You can call it like so:

    Code:
    [[MyWindowController sharedController] showWindow:nil];
    The first time you call sharedController, it loads the nib. Then all subsequent times it returns the same controller object.
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    Thanks thats great. Sorry for my ignorance, but what does the + do in front of the method ?

    Does that make it global ? or is that done by the use of static
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    The + indicates that the method is a class method.

    From The Objective-C Programming Language:

     
  5. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #5
    Even after following the same way, the window is not showing up.
    In IB, I connected delegate between window and controller also.
    What will be the reason for not showing the window?
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Did you connect the controller's window outlet to the window?
     
  7. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #7
    yes, i connected controllers outlet "window" and also windows delegate outlet to controller.

    implemented the code u mentioned.

    but it is not showing the window.

    I am attaching the sample project also. please go through that and suggest me the wrong I did in it. I am using XCode 2.4.1
     

    Attached Files:

  8. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #8
    I changed your code around a bit but your main problem was in interface builder, you have to set the File Owner's class to your "MyWindowCtrl" class.
     

    Attached Files:

  9. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #9
    Can you please tell me what are the changes you made and also I want to know how to implement this in XCode....please....
     
  10. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #10
    Your better off asking kainjow he is good at explain these things.

    The return type for the shared controller is id

    You had:

    + (MyWindowCtrl *)sharedInstanceValue

    I changed to:

    + (id )sharedInstanceValue

    Also as its a static variable you can declare it in the method. It lives for the duration of the programme.

    Im using XCode 3 so showing you the steps in Interface builder is a bit awkward as you have Xcode 2.
     
  11. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #11
    Can you please explain me Mr. Kainjowhow to do it in XCode 2.4.1
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    When you're using NSWindowControllers (actually any custom nib), you need to have the nib's File Owner be set as your window controller class. So to do this, in IB select the File Owner object in the nib. Then select Custom Class in the Inspector. Under the Custom Class list, scroll to MyWindowCtrl and select it. Now connect your "window" outlet like you did before, but connect it with the File's Owner object.
     
  13. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
  14. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #14
    Hi kainjow, i am facing few problems now.
    In my code, I have sharedController with init, a method "setData" which sets some variables in my class, a "windowDidBecomeMain" which sets default values of the controls in my UI.
    My UI has a combo box whose values are updated in "windowDidDecomeMain" and a OK button with appropriate IBAction handler.
    For showing up window, I am first creating the instance of class by called sharedController, then setData and then calling showWindow.

    Now the problem I am facing is, the data that I am setting using setData is not available in windowDidBecomeMain or IBAction handler for OK.

    When I traced, sharedController is called once, then init, then setData, again init, then windowDidBecomeMain.

    The error I am suspecting is that calling "Init" second time is resetting my class variables.

    What will be solution for this? Why Init is getting called again?
     
  15. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #15
    In the above code, I added dealloc method and releasing "instance" and making it nil. Also I am using [[self window]close] so that the window is closed and released. But next time, when I am creating sharedInstance and showing window, it is showing previous values in the textboxes I have in the window. Am I following right procedure?
     
  16. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK

Share This Page