Multiple XIBs connecting to the same Object?

Discussion in 'Mac Programming' started by BadWolf13, Jan 26, 2012.

  1. BadWolf13 macrumors 6502

    Dec 17, 2009
    Hey guys,

    I'm working on an Objective C project with multiple XIBs. To keep this project simple, I've put all of the processing within the myProjectAppDelegate file. To make the connection of all the IBOutlets to the view objects, I added an NSObject to each XIB and set its class to myProjectAppDelegate. I thought that it would link them all to the same instance of the AppDelegate, but it hasn't. It appears that it's creating a different instance for each of the XIB files. My question is, is it possible to link all three XIBs to the same instance of myProjectAppDelegate, or any class for that matter?
  2. Mac_Max macrumors 6502

    Mar 8, 2004
    I believe the architecture of Objective C/Cocoa on the Mac doesn't allow for that. The idea is one controller instance per model. What you could do is write your controller so that every instance of it works from the same instance of your model (dirty way: singleton, better way: database of some kind with transactions and locks). There are a LOT of issues that can come up when writing data if not done properly. If you make it so that only one view can manipulate the data at a time, it makes life a lot easier. You also might want to look into data binding the view and model together. Databinding isn't a "normal" OS X development thing but can be done. Its really been helpful to me on the Windows side of things for displaying and editing data from multiple views or multiple parts of the window.

    Cocoa Databinding:
  3. kainjow Moderator emeritus


    Jun 15, 2000
    Yes. Set the class on each of your nib's File's Owner object. Don't add a new NSObject object into the nib. That will create a new instance of your object. File's Owner acts as a proxy to whatever is creating the nib in code.
  4. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    Thanks, but I'm prevented from doing that right now. I've got the File's Owner set to NSViewController because I'm using the following line of code to load the XIB.

    	basicController = [[NSViewController alloc] initWithNibName:@"Basic" bundle:nil];
    I don't know of any other way to load a XIB file. Is there?
  5. kainjow Moderator emeritus


    Jun 15, 2000
  6. Sydde macrumors 68020


    Aug 17, 2009
    If you set the owner to the class of your app delegate in the nib file and specify your app delegate as the owner when you load the nib as kainjow suggests above, any outlets you define in your app delegate can be connected directly using the Files Owner in the nib itself. Create a different IBOutlet for each nib in your app delegate's ivars and the nib should connect directly to the appropriate outlet when it is instantiated.

    When you add a NSObject to a nib, an instance of that object will be created when the nib is loaded, so your app delegate would propagate instead of the original being used.
  7. rossipoo macrumors regular

    Jun 7, 2009
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

    If you want to communicate back to the app delegate, one way is to add a delegate property to the view controller(s) and have the app delegate set itself as a delegate to the view controller during awakeFromNib. Then the view controller can call its delegate methods when it needs to communicate back to the app delegate.
  8. jiminaus macrumors 65816


    Dec 16, 2010
    What do want to do with the app delegate?

    If you only want to connect actions to it, you can connect the actions to the First Responder pseudo-object. Your app delegate will be in the responder chain. It will be last in the responder chain though, so make the action unique application-wide to avoid an object earlier in the chain inadvertently responding to the action.

    If you only want to pull properties from the app delegate, set up an app delegate property in whatever you're File's Owner is. You can get the app delegate anywhere in your code using [NSApp delegate].

    If you want to use any of those app delegate properties in the UI, use Cocoa binding or setup the UI values in awakeFromNib:.
  9. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    jiminaus, I'm using the AppDelegate class as my controller, in classic MVC structure. XCode always creates that class, and I barely use any of the app delegate features, so I figure, why not give use it as my controller as well?

    I've got all my central processing and calculations happening in that class, with a model, and 3 XIBs. It's nicely organized.

    Kainjow, thank you, that worked beautifully. I created an outlet for the custom view in each XIB, and was able to get rid of the view controllers. Now it works perfectly.

Share This Page