Multiple nib files, some (well 2) questions

Discussion in 'Mac Programming' started by Eraserhead, Feb 13, 2007.

  1. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    After finding that having one nib file for everything in my program wasn't exactly the greatest idea I ever had I have a couple of questions as the documentation for mulitple nibs seems to be a bit lacking.

    1) If I have a controller in one file (Say MyDocument.nib) how to I access it in another nib file (Say AnotherNibFile.nib).

    2) So I don't have (!)'s in my nib (and to make stuff less confusing), I want to have two controllers to go with my two nib files (say MyDocController and AnotherNibFileController) for simplicity), now if MyDocController loads first, I can reference AnotherNibFileController in it, but how would I go back the other way?
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Accessing one controller from another is a matter of design. If you give us more specific examples it may help. Generally, I have a main controller class for the entire app, and that has other classes it manages (AppController -> WindowControllers, PreferencesController). So the main controller creates instances of the others. If the other controllers need to access something from the main controller, generally you can design it so they both don't have access to each other. It all depends on what you're actually doing in your code.
     
  3. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    To expand question 2, obviously in AppController I can put:
    Code:
    AnotherNibFileController *anfc=[[AnotherNibFileController alloc] init];
    
    And then I can call methods in anfc using [anfc someMethodInAnotherNibFileController];

    But how do I call an instance method in AppController (for example to get an arrayController, or a tabView) from the instance I have just created of AnotherNibFileController?

    I would need to go both ways, as I also need to access the array controller in most of my methods, which would return me to one nib/controller...
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You don't. In general this would be bad design. Note that you should be using initWithWindowNibName: otherwise the nib wouldn't get loaded. If you absolutely have to do what you are suggesting add an instance method to the subcontroller called registerParent:(id) parent or something similar and call this passing the main controller to setup the two way relationship.

    So you could do this in AppController
    Code:
    AnotherNibFileController *anfc=[[AnotherNibFileController alloc] initWithWindowNibName:@"AnotherWindow"];
    [anfc registerParent:self];
    
     
  5. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    So is their another (better) way to load an object in MyDocument.nib from AnotherNibFileController? Unless I import AnotherNibFileController into MyDocument.nib, which means I might as well stick everything in AppController, the problem with this is that then I have loads of undefined methods in AppController which don't link to anything in each nib which is very confusing to code...
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Perhaps if you give us some sort of concrete example of what you are trying to do we can give you a solution. You can't really do what you are trying to do. If you want to have the controls in two windows "talk" then the solution proper solution is to do it via the controller or model layer. If you use bindings then if the controls in one window update the model the other windows bound to that data will automatically update.
     
  7. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    OK, I think I need to come back to you on that, I'll try and think of another solution, thanks for your help :).
     

Share This Page